2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Taps Affs."""
|
2017-06-04 11:35:19 +00:00
|
|
|
from datetime import timedelta
|
2018-01-21 06:35:38 +00:00
|
|
|
import logging
|
2017-06-04 11:35:19 +00:00
|
|
|
|
2019-11-29 00:55:52 +00:00
|
|
|
from tapsaff import TapsAff
|
2017-06-04 11:35:19 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-06-04 11:35:19 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_LOCATION = "location"
|
2017-06-04 11:35:19 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Taps Aff"
|
2017-06-04 11:35:19 +00:00
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(minutes=30)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_LOCATION): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2017-06-04 11:35:19 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-06-04 11:35:19 +00:00
|
|
|
"""Set up the Taps Aff binary sensor."""
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
location = config.get(CONF_LOCATION)
|
|
|
|
|
|
|
|
taps_aff_data = TapsAffData(location)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([TapsAffSensor(taps_aff_data, name)], True)
|
2017-06-04 11:35:19 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class TapsAffSensor(BinarySensorEntity):
|
2017-06-04 11:35:19 +00:00
|
|
|
"""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."""
|
2019-09-03 19:12:51 +00:00
|
|
|
return f"{self._name}"
|
2017-06-04 11:35:19 +00:00
|
|
|
|
|
|
|
@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()
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class TapsAffData:
|
2017-06-04 11:35:19 +00:00
|
|
|
"""Class for handling the data retrieval for pins."""
|
|
|
|
|
|
|
|
def __init__(self, location):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize the data object."""
|
2017-06-04 11:35:19 +00:00
|
|
|
|
|
|
|
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")
|