From a8ddae9343683ca69067eecbece5ecff6d4e5d1d Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Wed, 25 Nov 2015 17:21:11 +0100 Subject: [PATCH 1/5] Add myStrom switch platform --- homeassistant/components/switch/mystrom.py | 103 +++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 homeassistant/components/switch/mystrom.py diff --git a/homeassistant/components/switch/mystrom.py b/homeassistant/components/switch/mystrom.py new file mode 100644 index 00000000000..86cd6686f1e --- /dev/null +++ b/homeassistant/components/switch/mystrom.py @@ -0,0 +1,103 @@ +""" +homeassistant.components.switch.mystrom +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Support for myStrom switches. + +For more details about this component, please refer to the documentation at +https://home-assistant.io/components/switch.mystrom/ +""" +import logging +import requests + +from homeassistant.components.switch import SwitchDevice +from homeassistant.const import STATE_UNKNOWN + +DEFAULT_NAME = 'myStrom Switch' + +_LOGGER = logging.getLogger(__name__) + + +# pylint: disable=unused-argument, too-many-function-args +def setup_platform(hass, config, add_devices, discovery_info=None): + """ Find and return myStrom switches. """ + resource = config.get('resource') + + if resource is None: + _LOGGER.error('Missing required variable: resource') + return False + + try: + requests.get(resource, timeout=10) + except requests.exceptions.MissingSchema: + _LOGGER.error("Missing resource or schema in configuration. " + "Add http:// to your URL.") + return False + except requests.exceptions.ConnectionError: + _LOGGER.error("No route to device. " + "Please check the IP address in the configuration file.") + return False + + add_devices([MyStromSwitch( + config.get('name', DEFAULT_NAME), + config.get('resource'))]) + + +class MyStromSwitch(SwitchDevice): + """ Represents a myStrom switch. """ + def __init__(self, name, resource): + self._state = STATE_UNKNOWN + self._name = name + self._resource = resource + self.consumption = 0 + + @property + def name(self): + """ The name of the switch. """ + return self._name + + @property + def is_on(self): + """ True if switch is on. """ + return self._state + + @property + def current_power_mwh(self): + """ Current power consumption in mwh. """ + return self.consumption + + def turn_on(self, **kwargs): + """ Turn the switch on. """ + request = requests.get('{}/relay'.format(self._resource), + params={'state': '1'}, + timeout=10) + if request.status_code == 200: + self._state = True + else: + _LOGGER.error("Can't turn on %s. Is device offline?", + self._resource) + + def turn_off(self, **kwargs): + """ Turn the switch off. """ + request = requests.get('{}/relay'.format(self._resource), + params={'state': '0'}, + timeout=10) + if request.status_code == 200: + self._state = False + else: + _LOGGER.error("Can't turn off %s. Is device offline?", + self._resource) + + def update(self): + """ Gets the latest data from REST API and updates the state. """ + try: + request = requests.get('{}/report'.format(self._resource), + timeout=10) + if request.json()['relay'] is True: + self._state = True + else: + self._state = False + + self.consumption = request.json()['power'] + except requests.exceptions.ConnectionError: + _LOGGER.error("No route to device '%s'. Is device offline?", + self._resource) From 9dfa4ea2339c0ff4303ddbd8540d56984f918b02 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Wed, 25 Nov 2015 17:21:31 +0100 Subject: [PATCH 2/5] Add mystrom switch --- .coveragerc | 1 + 1 file changed, 1 insertion(+) diff --git a/.coveragerc b/.coveragerc index 8efc162b3eb..22e8116693c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -98,6 +98,7 @@ omit = homeassistant/components/switch/command_switch.py homeassistant/components/switch/edimax.py homeassistant/components/switch/hikvisioncam.py + homeassistant/components/switch/mystrom.py homeassistant/components/switch/orvibo.py homeassistant/components/switch/rest.py homeassistant/components/switch/rpi_gpio.py From e45ef0013b3c7f551e84200c5f12e3b5b5d2229e Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Wed, 25 Nov 2015 18:07:29 +0100 Subject: [PATCH 3/5] Catch connection timeout --- homeassistant/components/switch/mystrom.py | 37 +++++++++++++--------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/switch/mystrom.py b/homeassistant/components/switch/mystrom.py index 86cd6686f1e..48f47b41ed6 100644 --- a/homeassistant/components/switch/mystrom.py +++ b/homeassistant/components/switch/mystrom.py @@ -10,7 +10,6 @@ import logging import requests from homeassistant.components.switch import SwitchDevice -from homeassistant.const import STATE_UNKNOWN DEFAULT_NAME = 'myStrom Switch' @@ -45,7 +44,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class MyStromSwitch(SwitchDevice): """ Represents a myStrom switch. """ def __init__(self, name, resource): - self._state = STATE_UNKNOWN + self._state = False self._name = name self._resource = resource self.consumption = 0 @@ -67,24 +66,32 @@ class MyStromSwitch(SwitchDevice): def turn_on(self, **kwargs): """ Turn the switch on. """ - request = requests.get('{}/relay'.format(self._resource), - params={'state': '1'}, - timeout=10) - if request.status_code == 200: - self._state = True - else: + try: + request = requests.get('{}/relay'.format(self._resource), + params={'state': '1'}, + timeout=10) + if request.status_code == 200: + self._state = True + else: + raise requests.exceptions.ConnectionError + + except requests.exceptions.ConnectionError: _LOGGER.error("Can't turn on %s. Is device offline?", self._resource) def turn_off(self, **kwargs): """ Turn the switch off. """ - request = requests.get('{}/relay'.format(self._resource), - params={'state': '0'}, - timeout=10) - if request.status_code == 200: - self._state = False - else: - _LOGGER.error("Can't turn off %s. Is device offline?", + try: + request = requests.get('{}/relay'.format(self._resource), + params={'state': '0'}, + timeout=10) + if request.status_code == 200: + self._state = False + else: + raise requests.exceptions.ConnectionError + + except requests.exceptions.ConnectionError: + _LOGGER.error("Can't turn on %s. Is device offline?", self._resource) def update(self): From 1bebb17e9ff5dc9b67a173b80bc76237a8273841 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Wed, 25 Nov 2015 18:14:19 +0100 Subject: [PATCH 4/5] Not need to raise exception --- homeassistant/components/switch/mystrom.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/homeassistant/components/switch/mystrom.py b/homeassistant/components/switch/mystrom.py index 48f47b41ed6..3acbfb24ce6 100644 --- a/homeassistant/components/switch/mystrom.py +++ b/homeassistant/components/switch/mystrom.py @@ -72,9 +72,6 @@ class MyStromSwitch(SwitchDevice): timeout=10) if request.status_code == 200: self._state = True - else: - raise requests.exceptions.ConnectionError - except requests.exceptions.ConnectionError: _LOGGER.error("Can't turn on %s. Is device offline?", self._resource) @@ -87,9 +84,6 @@ class MyStromSwitch(SwitchDevice): timeout=10) if request.status_code == 200: self._state = False - else: - raise requests.exceptions.ConnectionError - except requests.exceptions.ConnectionError: _LOGGER.error("Can't turn on %s. Is device offline?", self._resource) From f2f598bd264bb9536065058a65f49c49153f2920 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 26 Nov 2015 16:37:00 +0100 Subject: [PATCH 5/5] Use host as config var instead of resource --- homeassistant/components/switch/mystrom.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/switch/mystrom.py b/homeassistant/components/switch/mystrom.py index 3acbfb24ce6..36a62fe33e4 100644 --- a/homeassistant/components/switch/mystrom.py +++ b/homeassistant/components/switch/mystrom.py @@ -16,29 +16,27 @@ DEFAULT_NAME = 'myStrom Switch' _LOGGER = logging.getLogger(__name__) -# pylint: disable=unused-argument, too-many-function-args def setup_platform(hass, config, add_devices, discovery_info=None): """ Find and return myStrom switches. """ - resource = config.get('resource') + host = config.get('host') - if resource is None: - _LOGGER.error('Missing required variable: resource') + if host is None: + _LOGGER.error('Missing required variable: host') return False + resource = 'http://{}'.format(host) + try: requests.get(resource, timeout=10) - except requests.exceptions.MissingSchema: - _LOGGER.error("Missing resource or schema in configuration. " - "Add http:// to your URL.") - return False except requests.exceptions.ConnectionError: - _LOGGER.error("No route to device. " - "Please check the IP address in the configuration file.") + _LOGGER.error("No route to device %s. " + "Please check the IP address in the configuration file", + host) return False add_devices([MyStromSwitch( config.get('name', DEFAULT_NAME), - config.get('resource'))]) + resource)]) class MyStromSwitch(SwitchDevice):