|
| 1 | +"""Plugwise Binary Sensor component for Home Assistant.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Dict |
| 5 | + |
| 6 | +from homeassistant.components.binary_sensor import DEVICE_CLASS_HEAT, BinarySensorDevice |
| 7 | +from homeassistant.const import STATE_OFF, STATE_ON |
| 8 | +from homeassistant.core import callback |
| 9 | + |
| 10 | +from homeassistant.components.climate.const import ( |
| 11 | + CURRENT_HVAC_COOL, |
| 12 | + CURRENT_HVAC_HEAT, |
| 13 | + CURRENT_HVAC_IDLE, |
| 14 | +) |
| 15 | + |
| 16 | +from .const import ( |
| 17 | + CURRENT_HVAC_DHW, |
| 18 | + DOMAIN, |
| 19 | + COOL_ICON, |
| 20 | + FLAME_ICON, |
| 21 | + IDLE_ICON, |
| 22 | + WATER_ICON, |
| 23 | +) |
| 24 | + |
| 25 | +BINARY_SENSOR_LIST = [ |
| 26 | + "domestic_hot_water_state", |
| 27 | + "slave_boiler_state", |
| 28 | +] |
| 29 | + |
| 30 | +_LOGGER = logging.getLogger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +async def async_setup_entry(hass, config_entry, async_add_entities): |
| 34 | + """Set up the Smile binary_sensors from a config entry.""" |
| 35 | + api = hass.data[DOMAIN][config_entry.entry_id]["api"] |
| 36 | + updater = hass.data[DOMAIN][config_entry.entry_id]["updater"] |
| 37 | + |
| 38 | + devices = [] |
| 39 | + all_devices = api.get_all_devices() |
| 40 | + for dev_id, device in all_devices.items(): |
| 41 | + if device["class"] == "heater_central": |
| 42 | + _LOGGER.debug("Plugwise device_class found") |
| 43 | + data = api.get_device_data(dev_id) |
| 44 | + for binary_sensor in BINARY_SENSOR_LIST: |
| 45 | + _LOGGER.debug("Binary_sensor: %s", binary_sensor) |
| 46 | + if binary_sensor in data: |
| 47 | + _LOGGER.debug("Plugwise binary_sensor Dev %s", device["name"]) |
| 48 | + devices.append(PwBinarySensor(api, updater, binary_sensor, dev_id)) |
| 49 | + _LOGGER.info("Added binary_sensor.%s", binary_sensor) |
| 50 | + |
| 51 | + async_add_entities(devices, True) |
| 52 | + |
| 53 | + |
| 54 | +class PwBinarySensor(BinarySensorDevice): |
| 55 | + """Representation of a Plugwise binary_sensor.""" |
| 56 | + |
| 57 | + def __init__(self, api, updater, binary_sensor, dev_id): |
| 58 | + """Set up the Plugwise API.""" |
| 59 | + self._api = api |
| 60 | + self._updater = updater |
| 61 | + self._dev_id = dev_id |
| 62 | + self._binary_sensor = binary_sensor |
| 63 | + self._is_on = False |
| 64 | + self._unique_id = f"{dev_id}-binary_sensor" |
| 65 | + |
| 66 | + @property |
| 67 | + def unique_id(self): |
| 68 | + """Return a unique ID.""" |
| 69 | + return self._unique_id |
| 70 | + |
| 71 | + async def async_added_to_hass(self): |
| 72 | + """Register callbacks.""" |
| 73 | + self._updater.async_add_listener(self._update_callback) |
| 74 | + |
| 75 | + async def async_will_remove_from_hass(self): |
| 76 | + """Disconnect callbacks.""" |
| 77 | + self._updater.async_remove_listener(self._update_callback) |
| 78 | + |
| 79 | + @callback |
| 80 | + def _update_callback(self): |
| 81 | + """Call update method.""" |
| 82 | + self.update() |
| 83 | + self.async_write_ha_state() |
| 84 | + |
| 85 | + @property |
| 86 | + def name(self): |
| 87 | + """Return the name of the thermostat, if any.""" |
| 88 | + return self._binary_sensor |
| 89 | + |
| 90 | + @property |
| 91 | + def device_info(self) -> Dict[str, any]: |
| 92 | + """Return the device information.""" |
| 93 | + return { |
| 94 | + "identifiers": {(DOMAIN, self._dev_id)}, |
| 95 | + "name": self._binary_sensor, |
| 96 | + "manufacturer": "Plugwise", |
| 97 | + "via_device": (DOMAIN, self._api.gateway_id), |
| 98 | + } |
| 99 | + |
| 100 | + @property |
| 101 | + def should_poll(self): |
| 102 | + """No need to poll. Coordinator notifies entity of updates.""" |
| 103 | + return False |
| 104 | + |
| 105 | + @property |
| 106 | + def is_on(self): |
| 107 | + """Return true if the binary sensor is on.""" |
| 108 | + return self.is_on |
| 109 | + |
| 110 | + @property |
| 111 | + def state(self): |
| 112 | + """Return the state of the binary sensor.""" |
| 113 | + if self._is_on: |
| 114 | + return STATE_ON |
| 115 | + return STATE_OFF |
| 116 | + |
| 117 | + @property |
| 118 | + def icon(self): |
| 119 | + """Return the icon to use in the frontend.""" |
| 120 | + if self._binary_sensor == "domestic_hot_water_state": |
| 121 | + return WATER_ICON |
| 122 | + return FLAME_ICON |
| 123 | + |
| 124 | + @property |
| 125 | + def device_class(self): |
| 126 | + """Return the class of this device, from component DEVICE_CLASSES.""" |
| 127 | + return DEVICE_CLASS_HEAT |
| 128 | + |
| 129 | + def update(self): |
| 130 | + """Update the entity.""" |
| 131 | + _LOGGER.debug("Update binary_sensor called") |
| 132 | + data = self._api.get_device_data(self._dev_id) |
| 133 | + |
| 134 | + if data is None: |
| 135 | + _LOGGER.error("Received no data for device %s.", self._binary_sensor) |
| 136 | + else: |
| 137 | + if self._binary_sensor in data: |
| 138 | + self._is_on = data[self._binary_sensor] |
0 commit comments