From a1c119adb6aaceca787c6f714424468d20173c65 Mon Sep 17 00:00:00 2001 From: Barry Williams Date: Sun, 4 Jun 2017 12:35:19 +0100 Subject: [PATCH] Added a Taps Aff binary sensor (#7880) * Added a Taps Aff binary sensor * PR Review updates * Added a Taps Aff binary sensor * PR Review updates * Improved error handling * Cosmetic changes (ordering, docstings, etc.) --- .coveragerc | 1 + .../components/binary_sensor/tapsaff.py | 86 +++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 90 insertions(+) create mode 100644 homeassistant/components/binary_sensor/tapsaff.py diff --git a/.coveragerc b/.coveragerc index 3163b5f723c..1a8d7454bc8 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 00000000000..565abb73b36 --- /dev/null +++ b/homeassistant/components/binary_sensor/tapsaff.py @@ -0,0 +1,86 @@ +""" +Support for Taps Affs. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/binary_sensor.tapsaff/ +""" +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) + +REQUIREMENTS = ['tapsaff==0.1.3'] + +_LOGGER = logging.getLogger(__name__) + +CONF_LOCATION = 'location' + +DEFAULT_NAME = 'Taps Aff' + +SCAN_INTERVAL = timedelta(minutes=30) + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + 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 binary 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 binary sensor.""" + + def __init__(self, taps_aff_data, name): + """Initialize the Taps Aff 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 + + def update(self): + """Get the latest data from the Taps Aff API and updates the states.""" + try: + self._is_taps_aff = self.taps_aff.is_taps_aff + except RuntimeError: + _LOGGER.error("Update failed. Check configured location") diff --git a/requirements_all.txt b/requirements_all.txt index 1022c993435..2aa8d9c2f2f 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.3 + # homeassistant.components.tellstick # homeassistant.components.sensor.tellstick tellcore-py==1.1.2