From 178bf736f6c0f5a349af7f7d036fbd8764942d2e Mon Sep 17 00:00:00 2001 From: Heiko Thiery <heiko.thiery@gmail.com> Date: Thu, 4 Oct 2018 12:16:27 +0200 Subject: [PATCH] Add new component fritzbox binary_sensor (#17057) * Add new component fritzbox binary_sensor Signed-off-by: Heiko Thiery <heiko.thiery@gmail.com> * fix failed flake8 test Signed-off-by: Heiko Thiery <heiko.thiery@gmail.com> * add new file to .coveragerc Signed-off-by: Heiko Thiery <heiko.thiery@gmail.com> * use wildcard to cover all platforms Signed-off-by: Heiko Thiery <heiko.thiery@gmail.com> * remove polling because polling is true by default Signed-off-by: Heiko Thiery <heiko.thiery@gmail.com> * add blank line to keep imports ordered Signed-off-by: Heiko Thiery <heiko.thiery@gmail.com> * Minor changes --- .coveragerc | 2 +- .../components/binary_sensor/fritzbox.py | 64 +++++++++++++++++++ homeassistant/components/fritzbox.py | 2 +- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 homeassistant/components/binary_sensor/fritzbox.py diff --git a/.coveragerc b/.coveragerc index 1cd4c1774e4..378dba5ef2a 100644 --- a/.coveragerc +++ b/.coveragerc @@ -112,7 +112,7 @@ omit = homeassistant/components/*/evohome.py homeassistant/components/fritzbox.py - homeassistant/components/switch/fritzbox.py + homeassistant/components/*/fritzbox.py homeassistant/components/ecovacs.py homeassistant/components/*/ecovacs.py diff --git a/homeassistant/components/binary_sensor/fritzbox.py b/homeassistant/components/binary_sensor/fritzbox.py new file mode 100644 index 00000000000..ab58e6e84bc --- /dev/null +++ b/homeassistant/components/binary_sensor/fritzbox.py @@ -0,0 +1,64 @@ +""" +Support for Fritzbox binary sensors. + +For more details about this component, please refer to the documentation at +https://home-assistant.io/components/binary_sensor.fritzbox/ +""" +import logging + +import requests + +from homeassistant.components.binary_sensor import BinarySensorDevice +from homeassistant.components.fritzbox import DOMAIN as FRITZBOX_DOMAIN + +DEPENDENCIES = ['fritzbox'] + +_LOGGER = logging.getLogger(__name__) + + +def setup_platform(hass, config, add_entities, discovery_info=None): + """Set up the Fritzbox binary sensor platform.""" + devices = [] + fritz_list = hass.data[FRITZBOX_DOMAIN] + + for fritz in fritz_list: + device_list = fritz.get_devices() + for device in device_list: + if device.has_alarm: + devices.append(FritzboxBinarySensor(device, fritz)) + + add_entities(devices, True) + + +class FritzboxBinarySensor(BinarySensorDevice): + """Representation of a binary Fritzbox device.""" + + def __init__(self, device, fritz): + """Initialize the Fritzbox binary sensor.""" + self._device = device + self._fritz = fritz + + @property + def name(self): + """Return the name of the entity.""" + return self._device.name + + @property + def device_class(self): + """Return the class of this sensor.""" + return 'window' + + @property + def is_on(self): + """Return true if sensor is on.""" + if not self._device.present: + return False + return self._device.alert_state + + def update(self): + """Get latest data from the Fritzbox.""" + try: + self._device.update() + except requests.exceptions.HTTPError as ex: + _LOGGER.warning("Connection error: %s", ex) + self._fritz.login() diff --git a/homeassistant/components/fritzbox.py b/homeassistant/components/fritzbox.py index 49bc4c8f6e6..e6f121799df 100644 --- a/homeassistant/components/fritzbox.py +++ b/homeassistant/components/fritzbox.py @@ -18,7 +18,7 @@ _LOGGER = logging.getLogger(__name__) REQUIREMENTS = ['pyfritzhome==0.4.0'] -SUPPORTED_DOMAINS = ['climate', 'switch'] +SUPPORTED_DOMAINS = ['binary_sensor', 'climate', 'switch'] DOMAIN = 'fritzbox' -- GitLab