Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add start stop and restart services #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docker_monitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

UTILISATION_MONITOR_VERSION = 'utilization_version'

CONTAINER_NAME = 'container_name'
CONTAINER_MONITOR_STATUS = 'container_status'
CONTAINER_MONITOR_UPTIME = 'container_uptime'
CONTAINER_MONITOR_IMAGE = 'container_image'
Expand Down Expand Up @@ -105,6 +106,12 @@
})
}, extra=vol.ALLOW_EXTRA)

SERVICE_START = 'start'
SERVICE_STOP = 'stop'
SERVICE_RESTART = 'restart'
SERVICE_SCHEMA = vol.Schema({
vol.Required(CONTAINER_NAME): cv.string
})

def setup(hass, config):
_LOGGER.info("Settings: {}".format(config[DOMAIN]))
Expand Down Expand Up @@ -148,6 +155,29 @@ def event_listener(message):
if config[DOMAIN][CONF_EVENTS]:
api.events(event_listener)


def handle_start(call):
container_name = call.data[CONTAINER_NAME]
container = api.get_container(container_name)
if container:
container.start()

def handle_stop(call):
container_name = call.data[CONTAINER_NAME]
container = api.get_container(container_name)
if container:
container.stop()

def handle_restart(call):
container_name = call.data[CONTAINER_NAME]
container = api.get_container(container_name)
if container:
container.restart()

hass.services.register(DOMAIN, SERVICE_START, handle_start, SERVICE_SCHEMA)
hass.services.register(DOMAIN, SERVICE_STOP, handle_stop, SERVICE_SCHEMA)
hass.services.register(DOMAIN, SERVICE_RESTART, handle_restart, SERVICE_SCHEMA)

return True


Expand Down Expand Up @@ -297,6 +327,10 @@ def stop(self, timeout=10):
_LOGGER.info("Stop container {}".format(self._name))
self._container.stop(timeout=timeout)

def restart(self):
_LOGGER.info("Restart container {}".format(self._name))
self._container.restart()

def _notify(self, message):
_LOGGER.debug("Send notify for container {}".format(self._name))
for callback in self._subscribers:
Expand Down
19 changes: 19 additions & 0 deletions docker_monitor/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
start:
description: start a docker container
fields:
container_name:
description: name of the container to start.
example: docker_nginx_1
stop:
description: stop a docker container
fields:
container_name:
description: name of the container to stop.
example: docker_nginx_1
restart:
description: restart a docker container
fields:
container_name:
description: name of the container to restart.
example: docker_nginx_1

4 changes: 2 additions & 2 deletions docker_monitor/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.components.switch import (
ENTITY_ID_FORMAT,
PLATFORM_SCHEMA,
SwitchDevice
SwitchEntity
)
from homeassistant.const import (
ATTR_ATTRIBUTION,
Expand Down Expand Up @@ -49,7 +49,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
return False


class ContainerSwitch(SwitchDevice):
class ContainerSwitch(SwitchEntity):
def __init__(self, api, clientname, container_name):
self._api = api
self._clientname = clientname
Expand Down