From cf8e23adbc3fe477468a3370096ef88dea3f6f2b Mon Sep 17 00:00:00 2001 From: happyleaves Date: Sat, 14 Nov 2015 14:14:02 -0500 Subject: [PATCH 1/6] s20 switch support --- .coveragerc | 1 + homeassistant/components/switch/s20.py | 73 ++++++++++++++++++++++++++ requirements_all.txt | 3 ++ 3 files changed, 77 insertions(+) create mode 100644 homeassistant/components/switch/s20.py diff --git a/.coveragerc b/.coveragerc index 5202822b22f..c23238788a4 100644 --- a/.coveragerc +++ b/.coveragerc @@ -99,6 +99,7 @@ omit = homeassistant/components/switch/hikvisioncam.py homeassistant/components/switch/rest.py homeassistant/components/switch/rpi_gpio.py + homeassistant/components/switch/s20.py homeassistant/components/switch/transmission.py homeassistant/components/switch/wemo.py homeassistant/components/thermostat/honeywell.py diff --git a/homeassistant/components/switch/s20.py b/homeassistant/components/switch/s20.py new file mode 100644 index 00000000000..62b36721182 --- /dev/null +++ b/homeassistant/components/switch/s20.py @@ -0,0 +1,73 @@ +""" +homeassistant.components.switch.s20 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Support for Orvibo S20 Wifi Smart Switches. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/switch.s20/ +""" +import logging + +from homeassistant.components.switch import SwitchDevice + +from orvibo.s20 import S20, S20Exception + +DEFAULT_NAME = "Orbivo S20 Switch" +REQUIREMENTS = ['orbivo==1.0.0'] +_LOGGER = logging.getLogger(__name__) + + +# pylint: disable=unused-argument +def setup_platform(hass, config, add_devices_callback, discovery_info=None): + """ Find and return S20 switches. """ + if config.get('host') is None: + _LOGGER.error("Missing required variable: host") + return + try: + s20 = S20(config.get('host')) + add_devices_callback([S20Switch(config.get('name', DEFAULT_NAME), + s20)]) + except S20Exception as exception: + _LOGGER.error(exception) + + +class S20Switch(SwitchDevice): + """ Represents an S20 switch. """ + def __init__(self, name, s20): + self._name = name + self._s20 = s20 + + @property + def should_poll(self): + """ No polling needed. """ + return False + + @property + def name(self): + """ The name of the switch. """ + return self._name + + @property + def is_on(self): + """ True if device is on. """ + try: + return self._s20.on + except S20Exception as exception: + _LOGGER.error(exception) + return False + + def turn_on(self, **kwargs): + """ Turn the device on. """ + try: + self._s20.on = True + except S20Exception as exception: + _LOGGER.error(exception) + self.update_ha_state() + + def turn_off(self, **kwargs): + """ Turn the device off. """ + try: + self._s20.on = False + except S20Exception as exception: + _LOGGER.error(exception) + self.update_ha_state() diff --git a/requirements_all.txt b/requirements_all.txt index 571708ab873..7cb34b22c36 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -156,3 +156,6 @@ evohomeclient==0.2.3 # Pushetta (notify.pushetta) pushetta==1.0.15 + +# Orbivo S10 +orbivo==1.0.0 From 57ec58e255788bbf825b646d1f43748cd14a7141 Mon Sep 17 00:00:00 2001 From: happyleaves Date: Sat, 14 Nov 2015 14:19:47 -0500 Subject: [PATCH 2/6] fixed requirements --- requirements_all.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements_all.txt b/requirements_all.txt index 7cb34b22c36..907c79823bc 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -157,5 +157,5 @@ evohomeclient==0.2.3 # Pushetta (notify.pushetta) pushetta==1.0.15 -# Orbivo S10 -orbivo==1.0.0 +# Orvibo S10 +orvibo==1.0.0 From 70fef3c5b55ad2f28d6f42807fba95a91dbc2768 Mon Sep 17 00:00:00 2001 From: happyleaves Date: Sat, 14 Nov 2015 14:25:53 -0500 Subject: [PATCH 3/6] fixed names --- homeassistant/components/switch/s20.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/switch/s20.py b/homeassistant/components/switch/s20.py index 62b36721182..cefee79201c 100644 --- a/homeassistant/components/switch/s20.py +++ b/homeassistant/components/switch/s20.py @@ -12,8 +12,8 @@ from homeassistant.components.switch import SwitchDevice from orvibo.s20 import S20, S20Exception -DEFAULT_NAME = "Orbivo S20 Switch" -REQUIREMENTS = ['orbivo==1.0.0'] +DEFAULT_NAME = "Orvibo S20 Switch" +REQUIREMENTS = ['orvibo==1.0.0'] _LOGGER = logging.getLogger(__name__) From aabcad59d8d0c47d4261b518fa3c9f46594b9ec5 Mon Sep 17 00:00:00 2001 From: happyleaves Date: Sat, 14 Nov 2015 15:05:22 -0500 Subject: [PATCH 4/6] rename platform to orvibo --- .coveragerc | 2 +- homeassistant/components/switch/{s20.py => orvibo.py} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename homeassistant/components/switch/{s20.py => orvibo.py} (98%) diff --git a/.coveragerc b/.coveragerc index c23238788a4..f19e37d00a1 100644 --- a/.coveragerc +++ b/.coveragerc @@ -84,6 +84,7 @@ omit = homeassistant/components/sensor/glances.py homeassistant/components/sensor/mysensors.py homeassistant/components/sensor/openweathermap.py + homeassistant/components/switch/orvibo.py homeassistant/components/sensor/rest.py homeassistant/components/sensor/rpi_gpio.py homeassistant/components/sensor/sabnzbd.py @@ -99,7 +100,6 @@ omit = homeassistant/components/switch/hikvisioncam.py homeassistant/components/switch/rest.py homeassistant/components/switch/rpi_gpio.py - homeassistant/components/switch/s20.py homeassistant/components/switch/transmission.py homeassistant/components/switch/wemo.py homeassistant/components/thermostat/honeywell.py diff --git a/homeassistant/components/switch/s20.py b/homeassistant/components/switch/orvibo.py similarity index 98% rename from homeassistant/components/switch/s20.py rename to homeassistant/components/switch/orvibo.py index cefee79201c..909f1e89ce5 100644 --- a/homeassistant/components/switch/s20.py +++ b/homeassistant/components/switch/orvibo.py @@ -1,5 +1,5 @@ """ -homeassistant.components.switch.s20 +homeassistant.components.switch.orvibo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Support for Orvibo S20 Wifi Smart Switches. From 86b9ae95669d511ee6669d263e43b1d5ead5906a Mon Sep 17 00:00:00 2001 From: happyleaves Date: Sat, 14 Nov 2015 16:14:25 -0500 Subject: [PATCH 5/6] addressed comments --- homeassistant/components/switch/orvibo.py | 32 +++++++++++++---------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/switch/orvibo.py b/homeassistant/components/switch/orvibo.py index 909f1e89ce5..f53b131eabf 100644 --- a/homeassistant/components/switch/orvibo.py +++ b/homeassistant/components/switch/orvibo.py @@ -27,8 +27,8 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): s20 = S20(config.get('host')) add_devices_callback([S20Switch(config.get('name', DEFAULT_NAME), s20)]) - except S20Exception as exception: - _LOGGER.error(exception) + except S20Exception: + _LOGGER.exception("S20 couldn't be initialized") class S20Switch(SwitchDevice): @@ -36,11 +36,12 @@ class S20Switch(SwitchDevice): def __init__(self, name, s20): self._name = name self._s20 = s20 + self._state = False @property def should_poll(self): - """ No polling needed. """ - return False + """ Poll. """ + return True @property def name(self): @@ -50,24 +51,27 @@ class S20Switch(SwitchDevice): @property def is_on(self): """ True if device is on. """ + return self._state + + def update(self): + """ Update device state. """ try: - return self._s20.on - except S20Exception as exception: - _LOGGER.error(exception) - return False + self._state = self._s20.on + except S20Exception: + _LOGGER.exception("Error while fetching S20 state") def turn_on(self, **kwargs): """ Turn the device on. """ try: self._s20.on = True - except S20Exception as exception: - _LOGGER.error(exception) - self.update_ha_state() + self._state = True + except S20Exception: + _LOGGER.exception("Error while turning on S20") def turn_off(self, **kwargs): """ Turn the device off. """ try: self._s20.on = False - except S20Exception as exception: - _LOGGER.error(exception) - self.update_ha_state() + self._state = False + except S20Exception: + _LOGGER.exception("Error while turning off S20") From 12bdc392749c4faf1f2ea584d4335b9d816e3c69 Mon Sep 17 00:00:00 2001 From: happyleaves Date: Sun, 15 Nov 2015 08:58:15 -0500 Subject: [PATCH 6/6] don't update state in turn_on/off --- homeassistant/components/switch/orvibo.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/homeassistant/components/switch/orvibo.py b/homeassistant/components/switch/orvibo.py index f53b131eabf..c72c96e27bc 100644 --- a/homeassistant/components/switch/orvibo.py +++ b/homeassistant/components/switch/orvibo.py @@ -64,7 +64,6 @@ class S20Switch(SwitchDevice): """ Turn the device on. """ try: self._s20.on = True - self._state = True except S20Exception: _LOGGER.exception("Error while turning on S20") @@ -72,6 +71,5 @@ class S20Switch(SwitchDevice): """ Turn the device off. """ try: self._s20.on = False - self._state = False except S20Exception: _LOGGER.exception("Error while turning off S20")