Skip to content

Commit

Permalink
Merge branch 'feature/name'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanderhuisman committed Feb 18, 2019
2 parents 569f634 + 440dcc3 commit 8ffe6f5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ docker_monitor:
| Parameter | Type | Description |
| -------------------- | ------------------------ | --------------------------------------------------------------------- |
| 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. |
Expand Down
5 changes: 5 additions & 0 deletions docker_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from homeassistant.const import (
ATTR_ATTRIBUTION,
CONF_MONITORED_CONDITIONS,
CONF_NAME,
CONF_SCAN_INTERVAL,
CONF_URL,
EVENT_HOMEASSISTANT_STOP
Expand All @@ -36,6 +37,7 @@
PRECISION = 2

DEFAULT_URL = 'unix://var/run/docker.sock'
DEFAULT_NAME = 'Docker'

DEFAULT_SCAN_INTERVAL = timedelta(seconds=10)

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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),
Expand Down
22 changes: 13 additions & 9 deletions sensor/docker_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from homeassistant.const import (
ATTR_ATTRIBUTION,
CONF_MONITORED_CONDITIONS,
CONF_NAME,
CONF_SCAN_INTERVAL,
EVENT_HOMEASSISTANT_STOP
)
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
17 changes: 11 additions & 6 deletions switch/docker_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -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):
Expand Down

0 comments on commit 8ffe6f5

Please sign in to comment.