From c68e0a1d4646f6697f1972b99a5a5ad132f87d27 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 2 Jun 2020 18:30:00 +0200 Subject: [PATCH] Update plugwise to async and config_flow binary_sensor part (#36378) * Add binary_sensor component * Version bump * Blushing commit - tnx @bdraco --- .coveragerc | 1 + homeassistant/components/plugwise/__init__.py | 2 +- .../components/plugwise/binary_sensor.py | 96 +++++++++++++++++++ homeassistant/components/plugwise/const.py | 2 + .../components/plugwise/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 7 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 homeassistant/components/plugwise/binary_sensor.py diff --git a/.coveragerc b/.coveragerc index e17ba7a6503..41398ac5f0e 100644 --- a/.coveragerc +++ b/.coveragerc @@ -604,6 +604,7 @@ omit = homeassistant/components/plex/media_player.py homeassistant/components/plex/sensor.py homeassistant/components/plugwise/__init__.py + homeassistant/components/plugwise/binary_sensor.py homeassistant/components/plugwise/climate.py homeassistant/components/plugwise/sensor.py homeassistant/components/plum_lightpad/* diff --git a/homeassistant/components/plugwise/__init__.py b/homeassistant/components/plugwise/__init__.py index 610acfc3e23..abcb614253d 100644 --- a/homeassistant/components/plugwise/__init__.py +++ b/homeassistant/components/plugwise/__init__.py @@ -24,7 +24,7 @@ CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA) _LOGGER = logging.getLogger(__name__) SENSOR_PLATFORMS = ["sensor"] -ALL_PLATFORMS = ["climate", "sensor"] +ALL_PLATFORMS = ["binary_sensor", "climate", "sensor"] async def async_setup(hass: HomeAssistant, config: dict): diff --git a/homeassistant/components/plugwise/binary_sensor.py b/homeassistant/components/plugwise/binary_sensor.py new file mode 100644 index 00000000000..a2156cd37f9 --- /dev/null +++ b/homeassistant/components/plugwise/binary_sensor.py @@ -0,0 +1,96 @@ +"""Plugwise Binary Sensor component for Home Assistant.""" + +import logging + +from homeassistant.components.binary_sensor import BinarySensorEntity +from homeassistant.const import STATE_OFF, STATE_ON +from homeassistant.core import callback + +from .const import DOMAIN, FLAME_ICON, FLOW_OFF_ICON, FLOW_ON_ICON, IDLE_ICON +from .sensor import SmileSensor + +BINARY_SENSOR_MAP = { + "dhw_state": ["Domestic Hot Water State", None], + "slave_boiler_state": ["Secondary Heater Device State", None], +} + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry(hass, config_entry, async_add_entities): + """Set up the Smile binary_sensors from a config entry.""" + api = hass.data[DOMAIN][config_entry.entry_id]["api"] + coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"] + + entities = [] + + all_devices = api.get_all_devices() + for dev_id, device_properties in all_devices.items(): + if device_properties["class"] == "heater_central": + data = api.get_device_data(dev_id) + for binary_sensor, dummy in BINARY_SENSOR_MAP.items(): + if binary_sensor in data: + entities.append( + PwBinarySensor( + api, + coordinator, + device_properties["name"], + binary_sensor, + dev_id, + device_properties["class"], + ) + ) + + async_add_entities(entities, True) + + +class PwBinarySensor(SmileSensor, BinarySensorEntity): + """Representation of a Plugwise binary_sensor.""" + + def __init__(self, api, coordinator, name, binary_sensor, dev_id, model): + """Set up the Plugwise API.""" + super().__init__(api, coordinator, name, dev_id, binary_sensor) + + self._binary_sensor = binary_sensor + + self._is_on = False + self._icon = None + + self._unique_id = f"{dev_id}-{binary_sensor}" + + @property + def is_on(self): + """Return true if the binary sensor is on.""" + return self._is_on + + @property + def icon(self): + """Return the icon to use in the frontend.""" + return self._icon + + @callback + def _async_process_data(self): + """Update the entity.""" + data = self._api.get_device_data(self._dev_id) + + if not data: + _LOGGER.error("Received no data for device %s.", self._binary_sensor) + self.async_write_ha_state() + return + + if self._binary_sensor in data: + self._is_on = data[self._binary_sensor] + + self._state = STATE_OFF + if self._binary_sensor == "dhw_state": + self._icon = FLOW_OFF_ICON + if self._binary_sensor == "slave_boiler_state": + self._icon = IDLE_ICON + if self._is_on: + self._state = STATE_ON + if self._binary_sensor == "dhw_state": + self._icon = FLOW_ON_ICON + if self._binary_sensor == "slave_boiler_state": + self._icon = FLAME_ICON + + self.async_write_ha_state() diff --git a/homeassistant/components/plugwise/const.py b/homeassistant/components/plugwise/const.py index b57532b9192..9dc4c24b1e1 100644 --- a/homeassistant/components/plugwise/const.py +++ b/homeassistant/components/plugwise/const.py @@ -37,3 +37,5 @@ SCHEDULE_OFF = "false" COOL_ICON = "mdi:snowflake" FLAME_ICON = "mdi:fire" IDLE_ICON = "mdi:circle-off-outline" +FLOW_OFF_ICON = "mdi:water-pump-off" +FLOW_ON_ICON = "mdi:water-pump" diff --git a/homeassistant/components/plugwise/manifest.json b/homeassistant/components/plugwise/manifest.json index baecf485682..fa4cf32a2ec 100644 --- a/homeassistant/components/plugwise/manifest.json +++ b/homeassistant/components/plugwise/manifest.json @@ -2,7 +2,7 @@ "domain": "plugwise", "name": "Plugwise", "documentation": "https://www.home-assistant.io/integrations/plugwise", - "requirements": ["Plugwise_Smile==0.2.10"], + "requirements": ["Plugwise_Smile==0.2.13"], "codeowners": ["@CoMPaTech", "@bouwew"], "config_flow": true } diff --git a/requirements_all.txt b/requirements_all.txt index b40994c3df2..92477ae5ea9 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -44,7 +44,7 @@ Mastodon.py==1.5.1 OPi.GPIO==0.4.0 # homeassistant.components.plugwise -Plugwise_Smile==0.2.10 +Plugwise_Smile==0.2.13 # homeassistant.components.essent PyEssent==0.13 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 99f3c7a7163..0069aac21a5 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -7,7 +7,7 @@ HAP-python==2.9.1 # homeassistant.components.plugwise -Plugwise_Smile==0.2.10 +Plugwise_Smile==0.2.13 # homeassistant.components.flick_electric PyFlick==0.0.2