From e1e9d9261bc1bc9d8c69553b045dc2e89e87d2f3 Mon Sep 17 00:00:00 2001 From: Barry John Williams Date: Sun, 28 May 2017 13:16:05 +0100 Subject: [PATCH 1/6] Added a Taps Aff binary sensor --- .coveragerc | 1 + .../components/binary_sensor/tapsaff.py | 85 +++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 89 insertions(+) create mode 100644 homeassistant/components/binary_sensor/tapsaff.py diff --git a/.coveragerc b/.coveragerc index 3163b5f723c21b..1a8d7454bc8718 100644 --- a/.coveragerc +++ b/.coveragerc @@ -197,6 +197,7 @@ omit = homeassistant/components/binary_sensor/pilight.py homeassistant/components/binary_sensor/ping.py homeassistant/components/binary_sensor/rest.py + homeassistant/components/binary_sensor/tapsaff.py homeassistant/components/browser.py homeassistant/components/camera/amcrest.py homeassistant/components/camera/bloomsky.py diff --git a/homeassistant/components/binary_sensor/tapsaff.py b/homeassistant/components/binary_sensor/tapsaff.py new file mode 100644 index 00000000000000..7d01626600bb0a --- /dev/null +++ b/homeassistant/components/binary_sensor/tapsaff.py @@ -0,0 +1,85 @@ +""" +Support for Taps Aff binary sensor. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/binary_sensor.tapsaff/ +""" +from datetime import timedelta +import logging + +import voluptuous as vol + +from homeassistant.components.binary_sensor \ + import (BinarySensorDevice, PLATFORM_SCHEMA) +from homeassistant.const import (CONF_NAME) +import homeassistant.helpers.config_validation as cv +from homeassistant.util import Throttle + +REQUIREMENTS = ['tapsaff==0.1.0'] + +CONF_LOCATION = "location" + +_LOGGER = logging.getLogger(__name__) + +DEFAULT_NAME = 'Taps Aff' + +MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30) + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, + vol.Required(CONF_LOCATION): cv.string, +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up the taps aff sensor.""" + name = config.get(CONF_NAME) + location = config.get(CONF_LOCATION) + + taps_aff_data = TapsAffData(location) + + add_devices([TapsAffSensor(taps_aff_data, name)], True) + + +class TapsAffSensor(BinarySensorDevice): + """Implementation of a taps aff sensor.""" + + def __init__(self, taps_aff_data, name): + """Initialize the sensor.""" + self.data = taps_aff_data + self._name = name + + @property + def name(self): + """Return the name of the sensor.""" + return '{}'.format(self._name) + + @property + def is_on(self): + """Return true if taps aff.""" + return self.data.is_taps_aff + + def update(self): + """Get the latest data.""" + self.data.update() + + +class TapsAffData(object): + """Class for handling the data retrieval for pins.""" + + def __init__(self, location): + """Initialize the sensor.""" + from tapsaff import TapsAff + + self._is_taps_aff = None + self.taps_aff = TapsAff(location) + + @property + def is_taps_aff(self): + """Return true if taps aff.""" + return self._is_taps_aff + + @Throttle(MIN_TIME_BETWEEN_UPDATES) + def update(self): + """Get the latest data from the Taps Aff API and updates the states.""" + self._is_taps_aff = self.taps_aff.is_taps_aff diff --git a/requirements_all.txt b/requirements_all.txt index 00bce12fd07b98..d251aec2ca750a 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -805,6 +805,9 @@ statsd==3.2.1 # homeassistant.components.sensor.steam_online steamodd==4.21 +# homeassistant.components.binary_sensor.tapsaff +tapsaff==0.1.0 + # homeassistant.components.tellstick # homeassistant.components.sensor.tellstick tellcore-py==1.1.2 From f671649c1f28b04979ca1c3132c5c2f41b17e7a7 Mon Sep 17 00:00:00 2001 From: Barry John Williams Date: Sun, 4 Jun 2017 07:25:38 +0100 Subject: [PATCH 2/6] PR Review updates --- homeassistant/components/binary_sensor/tapsaff.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/binary_sensor/tapsaff.py b/homeassistant/components/binary_sensor/tapsaff.py index 7d01626600bb0a..b53bce800a2f24 100644 --- a/homeassistant/components/binary_sensor/tapsaff.py +++ b/homeassistant/components/binary_sensor/tapsaff.py @@ -9,11 +9,10 @@ import voluptuous as vol -from homeassistant.components.binary_sensor \ - import (BinarySensorDevice, PLATFORM_SCHEMA) +from homeassistant.components.binary_sensor import ( + BinarySensorDevice, PLATFORM_SCHEMA) from homeassistant.const import (CONF_NAME) import homeassistant.helpers.config_validation as cv -from homeassistant.util import Throttle REQUIREMENTS = ['tapsaff==0.1.0'] @@ -23,7 +22,7 @@ DEFAULT_NAME = 'Taps Aff' -MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30) +SCAN_INTERVAL = timedelta(minutes=30) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, @@ -79,7 +78,6 @@ def is_taps_aff(self): """Return true if taps aff.""" return self._is_taps_aff - @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Get the latest data from the Taps Aff API and updates the states.""" self._is_taps_aff = self.taps_aff.is_taps_aff From 7a9bfdf91f0f8d42b57287319eaad3f5451b09f1 Mon Sep 17 00:00:00 2001 From: Barry John Williams Date: Sun, 28 May 2017 13:16:05 +0100 Subject: [PATCH 3/6] Added a Taps Aff binary sensor --- .coveragerc | 1 + .../components/binary_sensor/tapsaff.py | 85 +++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 89 insertions(+) create mode 100644 homeassistant/components/binary_sensor/tapsaff.py diff --git a/.coveragerc b/.coveragerc index 3163b5f723c21b..1a8d7454bc8718 100644 --- a/.coveragerc +++ b/.coveragerc @@ -197,6 +197,7 @@ omit = homeassistant/components/binary_sensor/pilight.py homeassistant/components/binary_sensor/ping.py homeassistant/components/binary_sensor/rest.py + homeassistant/components/binary_sensor/tapsaff.py homeassistant/components/browser.py homeassistant/components/camera/amcrest.py homeassistant/components/camera/bloomsky.py diff --git a/homeassistant/components/binary_sensor/tapsaff.py b/homeassistant/components/binary_sensor/tapsaff.py new file mode 100644 index 00000000000000..7d01626600bb0a --- /dev/null +++ b/homeassistant/components/binary_sensor/tapsaff.py @@ -0,0 +1,85 @@ +""" +Support for Taps Aff binary sensor. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/binary_sensor.tapsaff/ +""" +from datetime import timedelta +import logging + +import voluptuous as vol + +from homeassistant.components.binary_sensor \ + import (BinarySensorDevice, PLATFORM_SCHEMA) +from homeassistant.const import (CONF_NAME) +import homeassistant.helpers.config_validation as cv +from homeassistant.util import Throttle + +REQUIREMENTS = ['tapsaff==0.1.0'] + +CONF_LOCATION = "location" + +_LOGGER = logging.getLogger(__name__) + +DEFAULT_NAME = 'Taps Aff' + +MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30) + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, + vol.Required(CONF_LOCATION): cv.string, +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up the taps aff sensor.""" + name = config.get(CONF_NAME) + location = config.get(CONF_LOCATION) + + taps_aff_data = TapsAffData(location) + + add_devices([TapsAffSensor(taps_aff_data, name)], True) + + +class TapsAffSensor(BinarySensorDevice): + """Implementation of a taps aff sensor.""" + + def __init__(self, taps_aff_data, name): + """Initialize the sensor.""" + self.data = taps_aff_data + self._name = name + + @property + def name(self): + """Return the name of the sensor.""" + return '{}'.format(self._name) + + @property + def is_on(self): + """Return true if taps aff.""" + return self.data.is_taps_aff + + def update(self): + """Get the latest data.""" + self.data.update() + + +class TapsAffData(object): + """Class for handling the data retrieval for pins.""" + + def __init__(self, location): + """Initialize the sensor.""" + from tapsaff import TapsAff + + self._is_taps_aff = None + self.taps_aff = TapsAff(location) + + @property + def is_taps_aff(self): + """Return true if taps aff.""" + return self._is_taps_aff + + @Throttle(MIN_TIME_BETWEEN_UPDATES) + def update(self): + """Get the latest data from the Taps Aff API and updates the states.""" + self._is_taps_aff = self.taps_aff.is_taps_aff diff --git a/requirements_all.txt b/requirements_all.txt index 1022c993435b7e..382b20bc1e4c23 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -805,6 +805,9 @@ statsd==3.2.1 # homeassistant.components.sensor.steam_online steamodd==4.21 +# homeassistant.components.binary_sensor.tapsaff +tapsaff==0.1.0 + # homeassistant.components.tellstick # homeassistant.components.sensor.tellstick tellcore-py==1.1.2 From 3f2372d43db8e8820e6a12aa2b43dee591d86c7f Mon Sep 17 00:00:00 2001 From: Barry John Williams Date: Sun, 4 Jun 2017 07:25:38 +0100 Subject: [PATCH 4/6] PR Review updates --- homeassistant/components/binary_sensor/tapsaff.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/binary_sensor/tapsaff.py b/homeassistant/components/binary_sensor/tapsaff.py index 7d01626600bb0a..b53bce800a2f24 100644 --- a/homeassistant/components/binary_sensor/tapsaff.py +++ b/homeassistant/components/binary_sensor/tapsaff.py @@ -9,11 +9,10 @@ import voluptuous as vol -from homeassistant.components.binary_sensor \ - import (BinarySensorDevice, PLATFORM_SCHEMA) +from homeassistant.components.binary_sensor import ( + BinarySensorDevice, PLATFORM_SCHEMA) from homeassistant.const import (CONF_NAME) import homeassistant.helpers.config_validation as cv -from homeassistant.util import Throttle REQUIREMENTS = ['tapsaff==0.1.0'] @@ -23,7 +22,7 @@ DEFAULT_NAME = 'Taps Aff' -MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30) +SCAN_INTERVAL = timedelta(minutes=30) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, @@ -79,7 +78,6 @@ def is_taps_aff(self): """Return true if taps aff.""" return self._is_taps_aff - @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Get the latest data from the Taps Aff API and updates the states.""" self._is_taps_aff = self.taps_aff.is_taps_aff From 56384ee4d83312390f5e19308e47fb89d6952f67 Mon Sep 17 00:00:00 2001 From: Barry John Williams Date: Sun, 4 Jun 2017 07:57:32 +0100 Subject: [PATCH 5/6] Improved error handling --- homeassistant/components/binary_sensor/tapsaff.py | 7 +++++-- requirements_all.txt | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/binary_sensor/tapsaff.py b/homeassistant/components/binary_sensor/tapsaff.py index b53bce800a2f24..c89518441bc4b0 100644 --- a/homeassistant/components/binary_sensor/tapsaff.py +++ b/homeassistant/components/binary_sensor/tapsaff.py @@ -14,7 +14,7 @@ from homeassistant.const import (CONF_NAME) import homeassistant.helpers.config_validation as cv -REQUIREMENTS = ['tapsaff==0.1.0'] +REQUIREMENTS = ['tapsaff==0.1.3'] CONF_LOCATION = "location" @@ -80,4 +80,7 @@ def is_taps_aff(self): def update(self): """Get the latest data from the Taps Aff API and updates the states.""" - self._is_taps_aff = self.taps_aff.is_taps_aff + try: + self._is_taps_aff = self.taps_aff.is_taps_aff + except RuntimeError: + logging.error("TapsAff Update failed, check configured location") diff --git a/requirements_all.txt b/requirements_all.txt index 382b20bc1e4c23..2aa8d9c2f2f92c 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -806,7 +806,7 @@ statsd==3.2.1 steamodd==4.21 # homeassistant.components.binary_sensor.tapsaff -tapsaff==0.1.0 +tapsaff==0.1.3 # homeassistant.components.tellstick # homeassistant.components.sensor.tellstick From 4c5a882424e6be5ede739027aa09e3a21a2f2148 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sun, 4 Jun 2017 13:25:10 +0200 Subject: [PATCH 6/6] Cosmetic changes (ordering, docstings, etc.) --- .../components/binary_sensor/tapsaff.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/binary_sensor/tapsaff.py b/homeassistant/components/binary_sensor/tapsaff.py index c89518441bc4b0..565abb73b36ec9 100644 --- a/homeassistant/components/binary_sensor/tapsaff.py +++ b/homeassistant/components/binary_sensor/tapsaff.py @@ -1,37 +1,37 @@ """ -Support for Taps Aff binary sensor. +Support for Taps Affs. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/binary_sensor.tapsaff/ """ -from datetime import timedelta import logging +from datetime import timedelta import voluptuous as vol +import homeassistant.helpers.config_validation as cv from homeassistant.components.binary_sensor import ( BinarySensorDevice, PLATFORM_SCHEMA) from homeassistant.const import (CONF_NAME) -import homeassistant.helpers.config_validation as cv REQUIREMENTS = ['tapsaff==0.1.3'] -CONF_LOCATION = "location" - _LOGGER = logging.getLogger(__name__) +CONF_LOCATION = 'location' + DEFAULT_NAME = 'Taps Aff' SCAN_INTERVAL = timedelta(minutes=30) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ - vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Required(CONF_LOCATION): cv.string, + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, }) def setup_platform(hass, config, add_devices, discovery_info=None): - """Set up the taps aff sensor.""" + """Set up the Taps Aff binary sensor.""" name = config.get(CONF_NAME) location = config.get(CONF_LOCATION) @@ -41,10 +41,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class TapsAffSensor(BinarySensorDevice): - """Implementation of a taps aff sensor.""" + """Implementation of a Taps Aff binary sensor.""" def __init__(self, taps_aff_data, name): - """Initialize the sensor.""" + """Initialize the Taps Aff sensor.""" self.data = taps_aff_data self._name = name @@ -83,4 +83,4 @@ def update(self): try: self._is_taps_aff = self.taps_aff.is_taps_aff except RuntimeError: - logging.error("TapsAff Update failed, check configured location") + _LOGGER.error("Update failed. Check configured location")