From 0edf5d9044af72a85fe850feadfa18f5560589ec Mon Sep 17 00:00:00 2001 From: Sander Huisman Date: Mon, 18 Feb 2019 17:35:08 +0100 Subject: [PATCH 1/2] Possibility to change prefix of entities --- README.md | 1 + docker_monitor.py | 5 +++++ sensor/docker_monitor.py | 22 +++++++++++++--------- switch/docker_monitor.py | 17 +++++++++++------ 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 11b7246..f8cbc30 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ docker_monitor: | Parameter | Type | Description | | -------------------- | ------------------------ | --------------------------------------------------------------------- | | url | string (Optional) | Host URL of Docker daemon. Defaults to `unix://var/run/docker.sock`. | +| name | string (Optional) | Client name of Docker daemon. Defaults to `Docker`. | | scan_interval | time_period (Optional) | Update interval. Defaults to 10 seconds. | | containers | list (Optional) | Array of containers to monitor. Defaults to all containers. | | monitored_conditions | list (Optional) | Array of conditions to be monitored. Defaults to all conditions | diff --git a/docker_monitor.py b/docker_monitor.py index b7bd15f..e0cd363 100644 --- a/docker_monitor.py +++ b/docker_monitor.py @@ -14,6 +14,7 @@ from homeassistant.const import ( ATTR_ATTRIBUTION, CONF_MONITORED_CONDITIONS, + CONF_NAME, CONF_SCAN_INTERVAL, CONF_URL, EVENT_HOMEASSISTANT_STOP @@ -36,6 +37,7 @@ PRECISION = 2 DEFAULT_URL = 'unix://var/run/docker.sock' +DEFAULT_NAME = 'Docker' DEFAULT_SCAN_INTERVAL = timedelta(seconds=10) @@ -82,6 +84,8 @@ CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({ + vol.Optional(CONF_NAME, default=DEFAULT_NAME): + cv.string, vol.Optional(CONF_URL, default=DEFAULT_URL): cv.string, vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): @@ -112,6 +116,7 @@ def setup(hass, config): hass.data[DOCKER_HANDLE] = {} hass.data[DOCKER_HANDLE][DATA_DOCKER_API] = api hass.data[DOCKER_HANDLE][DATA_CONFIG] = { + CONF_NAME: config[DOMAIN][CONF_NAME], CONF_CONTAINERS: config[DOMAIN].get(CONF_CONTAINERS, [container.get_name() for container in api.get_containers()]), CONF_MONITORED_CONDITIONS: config[DOMAIN].get(CONF_MONITORED_CONDITIONS), CONF_SCAN_INTERVAL: config[DOMAIN].get(CONF_SCAN_INTERVAL), diff --git a/sensor/docker_monitor.py b/sensor/docker_monitor.py index 97f488a..83f33c1 100644 --- a/sensor/docker_monitor.py +++ b/sensor/docker_monitor.py @@ -11,6 +11,7 @@ from homeassistant.const import ( ATTR_ATTRIBUTION, CONF_MONITORED_CONDITIONS, + CONF_NAME, CONF_SCAN_INTERVAL, EVENT_HOMEASSISTANT_STOP ) @@ -58,15 +59,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None): api = hass.data[DOCKER_HANDLE][DATA_DOCKER_API] config = hass.data[DOCKER_HANDLE][DATA_CONFIG] + clientname = config[CONF_NAME] interval = config[CONF_SCAN_INTERVAL].total_seconds() - sensors = [DockerUtilSensor(api, variable, interval) + sensors = [DockerUtilSensor(api, clientname, variable, interval) for variable in config[CONF_MONITORED_CONDITIONS] if variable in _UTILISATION_MON_COND] containers = [container.get_name() for container in api.get_containers()] for name in config[CONF_CONTAINERS]: if name in containers: - sensors += [DockerContainerSensor(api, name, variable, interval) + sensors += [DockerContainerSensor(api, clientname, name, variable, interval) for variable in config[CONF_MONITORED_CONDITIONS] if variable in _CONTAINER_MON_COND] if sensors: @@ -79,9 +81,10 @@ def setup_platform(hass, config, add_entities, discovery_info=None): class DockerUtilSensor(Entity): """Representation of a Docker Sensor.""" - def __init__(self, api, variable, interval): + def __init__(self, api, clientname, variable, interval): """Initialize the sensor.""" self._api = api + self._clientname = clientname self._interval = interval # TODO implement self._var_id = variable @@ -101,7 +104,7 @@ def __init__(self, api, variable, interval): @property def name(self): """Return the name of the sensor.""" - return "Docker {}".format(self._var_name) + return "{} {}".format(self._clientname, self._var_name) @property def icon(self): @@ -142,10 +145,11 @@ def device_state_attributes(self): class DockerContainerSensor(Entity): """Representation of a Docker Sensor.""" - def __init__(self, api, name, variable, interval): + def __init__(self, api, clientname, container_name, variable, interval): """Initialize the sensor.""" self._api = api - self._name = name + self._clientname = clientname + self._container_name = container_name self._interval = interval self._var_id = variable @@ -159,10 +163,10 @@ def __init__(self, api, name, variable, interval): ATTR_ATTRIBUTION: CONF_ATTRIBUTION } - self._container = api.get_container(name) + self._container = api.get_container(container_name) _LOGGER.info("Initializing Docker sensor \"{}\" with parameter: {}".format( - self._name, self._var_name)) + self._container_name, self._var_name)) def update_callback(stats): state = None @@ -230,7 +234,7 @@ def update_callback(stats): @property def name(self): """Return the name of the sensor, if any.""" - return "Docker {} {}".format(self._name, self._var_name) + return "{} {} {}".format(self._clientname, self._container_name, self._var_name) @property def icon(self): diff --git a/switch/docker_monitor.py b/switch/docker_monitor.py index 00b9025..7f53509 100644 --- a/switch/docker_monitor.py +++ b/switch/docker_monitor.py @@ -11,7 +11,10 @@ PLATFORM_SCHEMA, SwitchDevice ) -from homeassistant.const import ATTR_ATTRIBUTION +from homeassistant.const import ( + ATTR_ATTRIBUTION, + CONF_NAME +) from homeassistant.core import ServiceCall from custom_components.docker_monitor import ( @@ -32,9 +35,10 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): api = hass.data[DOCKER_HANDLE][DATA_DOCKER_API] config = hass.data[DOCKER_HANDLE][DATA_CONFIG] + clientname = config[CONF_NAME] containers = [container.get_name() for container in api.get_containers()] - switches = [ContainerSwitch(api, name) + switches = [ContainerSwitch(api, clientname, name) for name in config[CONF_CONTAINERS] if name in containers] if switches: add_devices_callback(switches, True) @@ -44,12 +48,13 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): class ContainerSwitch(SwitchDevice): - def __init__(self, api, name): + def __init__(self, api, clientname, container_name): self._api = api - self._name = name + self._clientname = clientname + self._container_name = container_name self._state = False - self._container = api.get_container(name) + self._container = api.get_container(container_name) def update_callback(stats): _LOGGER.debug("Received callback with message: {}".format(stats)) @@ -69,7 +74,7 @@ def update_callback(stats): @property def name(self): """Return the name of the sensor.""" - return "Docker {}".format(self._name) + return "{} {}".format(self._clientname, self._container_name) @property def should_poll(self): From 440dcc31a061dc3518bf5c3212ee692ed8d598bd Mon Sep 17 00:00:00 2001 From: Sander Huisman Date: Mon, 18 Feb 2019 17:40:25 +0100 Subject: [PATCH 2/2] Updated order of config to match code --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 74c4fe2..161a215 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,8 @@ docker_monitor: | Parameter | Type | Description | | -------------------- | ------------------------ | --------------------------------------------------------------------- | -| url | string (Optional) | Host URL of Docker daemon. Defaults to `unix://var/run/docker.sock`. | | name | string (Optional) | Client name of Docker daemon. Defaults to `Docker`. | +| url | string (Optional) | Host URL of Docker daemon. Defaults to `unix://var/run/docker.sock`. | | scan_interval | time_period (Optional) | Update interval. Defaults to 10 seconds. | | containers | list (Optional) | Array of containers to monitor. Defaults to all containers. | | monitored_conditions | list (Optional) | Array of conditions to be monitored. Defaults to all conditions |