Skip to content

Commit

Permalink
Merge pull request #140 from chris-mc1/master
Browse files Browse the repository at this point in the history
 Converted to full asyncio
  • Loading branch information
ualex73 authored Dec 27, 2023
2 parents 733bc79 + e3daba7 commit 5ca0d13
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 352 deletions.
36 changes: 12 additions & 24 deletions custom_components/monitor_docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

import asyncio
import logging
import time
import threading
import voluptuous as vol

from datetime import timedelta

import homeassistant.helpers.config_validation as cv

from .helpers import DockerAPI

import voluptuous as vol
from homeassistant.const import (
CONF_MONITORED_CONDITIONS,
CONF_NAME,
CONF_SCAN_INTERVAL,
CONF_URL,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType

from .const import (
API,
Expand All @@ -38,14 +34,15 @@
CONF_SWITCHNAME,
CONFIG,
CONTAINER_INFO_ALLINONE,
DOMAIN,
DEFAULT_NAME,
DEFAULT_RETRY,
DEFAULT_SENSORNAME,
DEFAULT_SWITCHNAME,
DOMAIN,
MONITORED_CONDITIONS_LIST,
PRECISION,
)
from .helpers import DockerAPI

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -90,17 +87,12 @@


#################################################################
async def async_setup(hass, config):
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Will setup the Monitor Docker platform."""

def RunDocker(hass, entry):
async def RunDocker(hass: HomeAssistant, entry: ConfigType) -> None:
"""Wrapper around function for a separated thread."""

# Create out asyncio loop, because we are already inside
# a def (not main) we need to do create/set
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

# Create docker instance, it will have asyncio threads
hass.data[DOMAIN][entry[CONF_NAME]] = {}
hass.data[DOMAIN][entry[CONF_NAME]][CONFIG] = entry
Expand All @@ -111,23 +103,22 @@ def RunDocker(hass, entry):
doLoop = True

try:
hass.data[DOMAIN][entry[CONF_NAME]][API] = DockerAPI(
hass, entry, startCount
)
hass.data[DOMAIN][entry[CONF_NAME]][API] = DockerAPI(hass, entry)
await hass.data[DOMAIN][entry[CONF_NAME]][API].init(startCount)
except Exception as err:
doLoop = False
if entry[CONF_RETRY] == 0:
raise
else:
_LOGGER.error("Failed Docker connect: %s", str(err))
_LOGGER.error("Retry in %d seconds", entry[CONF_RETRY])
time.sleep(entry[CONF_RETRY])
await asyncio.sleep(entry[CONF_RETRY])

startCount += 1

if doLoop:
# Now run forever in this separated thread
loop.run_forever()
# loop.run_forever()

# We only get here if a docker instance disconnected or HASS is stopping
if not hass.data[DOMAIN][entry[CONF_NAME]][API]._dockerStopped:
Expand Down Expand Up @@ -156,9 +147,6 @@ def RunDocker(hass, entry):
return False

# Each docker hosts runs in its own thread. We need to pass hass too, for the load_platform
thread = threading.Thread(
target=RunDocker, kwargs={"hass": hass, "entry": entry}
)
thread.start()
asyncio.create_task(RunDocker(hass, entry))

return True
185 changes: 149 additions & 36 deletions custom_components/monitor_docker/const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
"""Define constants for the Monitor Docker component."""

from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import PERCENTAGE, UnitOfDataRate, UnitOfInformation

DOMAIN = "monitor_docker"
API = "api"
CONFIG = "config"
Expand Down Expand Up @@ -60,44 +67,150 @@
CONTAINER_STATS_NETWORK_TOTAL_DOWN = "network_total_down"

DOCKER_MONITOR_LIST = {
DOCKER_INFO_VERSION: ["Version", None, "mdi:information-outline", None, None],
DOCKER_INFO_CONTAINER_RUNNING: ["Containers Running", None, "mdi:docker", None, None],
DOCKER_INFO_CONTAINER_PAUSED: ["Containers Paused", None, "mdi:docker", None, None],
DOCKER_INFO_CONTAINER_STOPPED: ["Containers Stopped", None, "mdi:docker", None, None],
DOCKER_INFO_CONTAINER_TOTAL: ["Containers Total", None, "mdi:docker", None, None],
DOCKER_STATS_CPU_PERCENTAGE: ["CPU", "%", "mdi:chip", None, "measurement"],
DOCKER_STATS_1CPU_PERCENTAGE: ["1CPU", "%", "mdi:chip", None, "measurement"],
DOCKER_STATS_MEMORY: ["Memory", "MB", "mdi:memory", None, "measurement"],
DOCKER_STATS_MEMORY_PERCENTAGE: ["Memory (percent)", "%", "mdi:memory", None, "measurement"],
DOCKER_INFO_IMAGES: ["Images", None, "mdi:docker", None, None],
DOCKER_INFO_VERSION: SensorEntityDescription(
key=DOCKER_INFO_VERSION,
name="Version",
icon="mdi:information-outline",
),
DOCKER_INFO_CONTAINER_RUNNING: SensorEntityDescription(
key=DOCKER_INFO_CONTAINER_RUNNING,
name="Containers Running",
icon="mdi:docker",
),
DOCKER_INFO_CONTAINER_PAUSED: SensorEntityDescription(
key=DOCKER_INFO_CONTAINER_PAUSED,
name="Containers Paused",
icon="mdi:docker",
),
DOCKER_INFO_CONTAINER_STOPPED: SensorEntityDescription(
key=DOCKER_INFO_CONTAINER_STOPPED,
name="Containers Stopped",
icon="mdi:docker",
),
DOCKER_INFO_CONTAINER_TOTAL: SensorEntityDescription(
key=DOCKER_INFO_CONTAINER_TOTAL,
name="Containers Total",
icon="mdi:docker",
),
DOCKER_STATS_CPU_PERCENTAGE: SensorEntityDescription(
key=DOCKER_STATS_CPU_PERCENTAGE,
name="CPU",
native_unit_of_measurement=PERCENTAGE,
icon="mdi:chip",
state_class=SensorStateClass.MEASUREMENT,
),
DOCKER_STATS_1CPU_PERCENTAGE: SensorEntityDescription(
key=DOCKER_STATS_1CPU_PERCENTAGE,
name="1CPU",
native_unit_of_measurement=PERCENTAGE,
icon="mdi:chip",
state_class=SensorStateClass.MEASUREMENT,
),
DOCKER_STATS_MEMORY: SensorEntityDescription(
key=DOCKER_STATS_MEMORY,
name="Memory",
native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
icon="mdi:memory",
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
),
DOCKER_STATS_MEMORY_PERCENTAGE: SensorEntityDescription(
key=DOCKER_STATS_MEMORY_PERCENTAGE,
name="Memory (percent)",
native_unit_of_measurement=PERCENTAGE,
icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT,
),
DOCKER_INFO_IMAGES: SensorEntityDescription(
key=DOCKER_INFO_IMAGES,
name="Images",
icon="mdi:docker",
),
}

CONTAINER_MONITOR_LIST = {
CONTAINER_INFO_STATE: ["State", None, "mdi:checkbox-marked-circle-outline", None, None],
CONTAINER_INFO_HEALTH: ["Health", None, "mdi:heart-pulse", None, None],
CONTAINER_INFO_STATUS: ["Status", None, "mdi:checkbox-marked-circle-outline", None, None],
CONTAINER_INFO_UPTIME: ["Up Time", "", "mdi:clock", "timestamp", None],
CONTAINER_INFO_IMAGE: ["Image", None, "mdi:information-outline", None, None],
CONTAINER_STATS_CPU_PERCENTAGE: ["CPU", "%", "mdi:chip", None, "measurement"],
CONTAINER_STATS_1CPU_PERCENTAGE: ["1CPU", "%", "mdi:chip", None, "measurement"],
CONTAINER_STATS_MEMORY: ["Memory", "MB", "mdi:memory", None, "measurement"],
CONTAINER_STATS_MEMORY_PERCENTAGE: ["Memory (percent)", "%", "mdi:memory", None, "measurement"],
CONTAINER_STATS_NETWORK_SPEED_UP: ["Network speed Up", "kB/s", "mdi:upload", None, "measurement"],
CONTAINER_STATS_NETWORK_SPEED_DOWN: [
"Network speed Down",
"kB/s",
"mdi:download",
None,
"measurement"
],
CONTAINER_STATS_NETWORK_TOTAL_UP: ["Network total Up", "MB", "mdi:upload", None, "total_increasing"],
CONTAINER_STATS_NETWORK_TOTAL_DOWN: [
"Network total Down",
"MB",
"mdi:download",
None,
"total_increasing"
],
CONTAINER_INFO_STATE: SensorEntityDescription(
key=CONTAINER_INFO_STATE,
name="State",
icon="mdi:checkbox-marked-circle-outline",
),
CONTAINER_INFO_HEALTH: SensorEntityDescription(
key=CONTAINER_INFO_HEALTH,
name="Health",
icon="mdi:heart-pulse",
),
CONTAINER_INFO_STATUS: SensorEntityDescription(
key=CONTAINER_INFO_STATUS,
name="Status",
icon="mdi:checkbox-marked-circle-outline",
),
CONTAINER_INFO_UPTIME: SensorEntityDescription(
key=CONTAINER_INFO_UPTIME,
name="Up Time",
icon="mdi:clock",
device_class=SensorDeviceClass.TIMESTAMP,
),
CONTAINER_INFO_IMAGE: SensorEntityDescription(
key=CONTAINER_INFO_IMAGE,
name="Image",
icon="mdi:information-outline",
),
CONTAINER_STATS_CPU_PERCENTAGE: SensorEntityDescription(
key=CONTAINER_STATS_CPU_PERCENTAGE,
name="CPU",
native_unit_of_measurement=PERCENTAGE,
icon="mdi:chip",
state_class=SensorStateClass.MEASUREMENT,
),
CONTAINER_STATS_1CPU_PERCENTAGE: SensorEntityDescription(
key=CONTAINER_STATS_1CPU_PERCENTAGE,
name="1CPU",
native_unit_of_measurement=PERCENTAGE,
icon="mdi:chip",
state_class=SensorStateClass.MEASUREMENT,
),
CONTAINER_STATS_MEMORY: SensorEntityDescription(
key=CONTAINER_STATS_MEMORY,
name="Memory",
native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT,
),
CONTAINER_STATS_MEMORY_PERCENTAGE: SensorEntityDescription(
key=CONTAINER_STATS_MEMORY_PERCENTAGE,
name="Memory (percent)",
native_unit_of_measurement=PERCENTAGE,
icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT,
),
CONTAINER_STATS_NETWORK_SPEED_UP: SensorEntityDescription(
key=CONTAINER_STATS_NETWORK_SPEED_UP,
name="Network speed Up",
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
icon="mdi:upload",
state_class=SensorStateClass.MEASUREMENT,
),
CONTAINER_STATS_NETWORK_SPEED_DOWN: SensorEntityDescription(
key=CONTAINER_STATS_NETWORK_SPEED_DOWN,
name="Network speed Down",
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
icon="mdi:download",
state_class=SensorStateClass.MEASUREMENT,
),
CONTAINER_STATS_NETWORK_TOTAL_UP: SensorEntityDescription(
key=CONTAINER_STATS_NETWORK_TOTAL_UP,
name="Network total Up",
native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
icon="mdi:upload",
state_class=SensorStateClass.TOTAL_INCREASING,
),
CONTAINER_STATS_NETWORK_TOTAL_DOWN: SensorEntityDescription(
key=CONTAINER_STATS_NETWORK_TOTAL_DOWN,
name="Network total Down",
native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
icon="mdi:download",
state_class=SensorStateClass.TOTAL_INCREASING,
),
}

CONTAINER_MONITOR_NETWORK_LIST = [
Expand All @@ -118,4 +231,4 @@
ATTR_VERSION_ARCH = "Architecture"
ATTR_VERSION_KERNEL = "Kernel"
ATTR_VERSION_OS = "OS"
ATTR_VERSION_OS_TYPE = "OS_Type"
ATTR_VERSION_OS_TYPE = "OS_Type"
Loading

0 comments on commit 5ca0d13

Please sign in to comment.