Add notification binary_sensor to Plugwise integration (#41473)
* Notifications extract from beta * Remove info loggings * Delete notification service * Only notifications for right smiles * Revert to correct logic * Catchup with dev (mostly with ourselves from #41201) * Remove debug logging * Naming improvement * Improve test quality as per codecov patch requirement * Revert to original condition (and appropriately test) * Fix delete_notification_service, bring tests fixtures up to 1.6.0 including notifications * Review comment (@bouwew) * Correct test value * Re-apply #40108 fix after rebase sidestep * Update tests/components/plugwise/test_init.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Add needed state to imports * Remove separate gw unload code * Change entry_fail approach * Revert persistent notification part * Revert persistent notification part - lint * Update homeassistant/components/plugwise/binary_sensor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/plugwise/binary_sensor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Rework reuse of sensor in binary_sensor * Explicit state attribute keys * Remove tempfile * List of notifications per severity * Update homeassistant/components/plugwise/binary_sensor.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Chris Talkington <chris@talkingtontech.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
c8113e6b11
commit
ed36cb7beb
25 changed files with 152 additions and 32 deletions
|
@ -3,7 +3,6 @@
|
|||
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 (
|
||||
|
@ -13,13 +12,16 @@ from .const import (
|
|||
FLOW_OFF_ICON,
|
||||
FLOW_ON_ICON,
|
||||
IDLE_ICON,
|
||||
NO_NOTIFICATION_ICON,
|
||||
NOTIFICATION_ICON,
|
||||
)
|
||||
from .sensor import SmileSensor
|
||||
from .gateway import SmileGateway
|
||||
|
||||
BINARY_SENSOR_MAP = {
|
||||
"dhw_state": ["Domestic Hot Water State", None],
|
||||
"slave_boiler_state": ["Secondary Heater Device State", None],
|
||||
}
|
||||
SEVERITIES = ["other", "info", "warning", "error"]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -30,12 +32,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
|
||||
|
||||
entities = []
|
||||
is_thermostat = api.single_master_thermostat()
|
||||
|
||||
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 in BINARY_SENSOR_MAP:
|
||||
if binary_sensor not in data:
|
||||
continue
|
||||
|
@ -47,32 +50,65 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
device_properties["name"],
|
||||
dev_id,
|
||||
binary_sensor,
|
||||
device_properties["class"],
|
||||
)
|
||||
)
|
||||
|
||||
if device_properties["class"] == "gateway" and is_thermostat is not None:
|
||||
entities.append(
|
||||
PwNotifySensor(
|
||||
api,
|
||||
coordinator,
|
||||
device_properties["name"],
|
||||
dev_id,
|
||||
"plugwise_notification",
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities, True)
|
||||
|
||||
|
||||
class PwBinarySensor(SmileSensor, BinarySensorEntity):
|
||||
"""Representation of a Plugwise binary_sensor."""
|
||||
class SmileBinarySensor(SmileGateway):
|
||||
"""Represent Smile Binary Sensors."""
|
||||
|
||||
def __init__(self, api, coordinator, name, dev_id, binary_sensor, model):
|
||||
"""Set up the Plugwise API."""
|
||||
super().__init__(api, coordinator, name, dev_id, binary_sensor)
|
||||
def __init__(self, api, coordinator, name, dev_id, binary_sensor):
|
||||
"""Initialise the binary_sensor."""
|
||||
super().__init__(api, coordinator, name, dev_id)
|
||||
|
||||
self._binary_sensor = binary_sensor
|
||||
|
||||
self._is_on = False
|
||||
self._icon = None
|
||||
self._is_on = False
|
||||
|
||||
if dev_id == self._api.heater_id:
|
||||
self._entity_name = "Auxiliary"
|
||||
|
||||
sensorname = binary_sensor.replace("_", " ").title()
|
||||
self._name = f"{self._entity_name} {sensorname}"
|
||||
|
||||
if dev_id == self._api.gateway_id:
|
||||
self._entity_name = f"Smile {self._entity_name}"
|
||||
|
||||
self._unique_id = f"{dev_id}-{binary_sensor}"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon of this entity."""
|
||||
return self._icon
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the binary sensor is on."""
|
||||
return self._is_on
|
||||
|
||||
@callback
|
||||
def _async_process_data(self):
|
||||
"""Update the entity."""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class PwBinarySensor(SmileBinarySensor, BinarySensorEntity):
|
||||
"""Representation of a Plugwise binary_sensor."""
|
||||
|
||||
@callback
|
||||
def _async_process_data(self):
|
||||
"""Update the entity."""
|
||||
|
@ -89,10 +125,48 @@ class PwBinarySensor(SmileSensor, BinarySensorEntity):
|
|||
|
||||
self._is_on = data[self._binary_sensor]
|
||||
|
||||
self._state = STATE_ON if self._is_on else STATE_OFF
|
||||
if self._binary_sensor == "dhw_state":
|
||||
self._icon = FLOW_ON_ICON if self._is_on else FLOW_OFF_ICON
|
||||
if self._binary_sensor == "slave_boiler_state":
|
||||
self._icon = FLAME_ICON if self._is_on else IDLE_ICON
|
||||
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class PwNotifySensor(SmileBinarySensor, BinarySensorEntity):
|
||||
"""Representation of a Plugwise Notification binary_sensor."""
|
||||
|
||||
def __init__(self, api, coordinator, name, dev_id, binary_sensor):
|
||||
"""Set up the Plugwise API."""
|
||||
super().__init__(api, coordinator, name, dev_id, binary_sensor)
|
||||
|
||||
self._attributes = {}
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return self._attributes
|
||||
|
||||
@callback
|
||||
def _async_process_data(self):
|
||||
"""Update the entity."""
|
||||
notify = self._api.notifications
|
||||
|
||||
for severity in SEVERITIES:
|
||||
self._attributes[f"{severity}_msg"] = []
|
||||
|
||||
self._is_on = False
|
||||
self._icon = NO_NOTIFICATION_ICON
|
||||
|
||||
if notify:
|
||||
self._is_on = True
|
||||
self._icon = NOTIFICATION_ICON
|
||||
|
||||
for details in notify.values():
|
||||
for msg_type, msg in details.items():
|
||||
if msg_type not in SEVERITIES:
|
||||
msg_type = "other"
|
||||
|
||||
self._attributes[f"{msg_type.lower()}_msg"].append(msg)
|
||||
|
||||
self.async_write_ha_state()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue