11"""Plugwise Binary Sensor component for Home Assistant."""
22
33import logging
4- from typing import Dict
54
6- from homeassistant .components .binary_sensor import (
7- DEVICE_CLASS_OPENING ,
8- BinarySensorEntity ,
9- )
5+ from homeassistant .components .binary_sensor import BinarySensorEntity , DEVICE_CLASS_OPENING
106from homeassistant .const import STATE_OFF , STATE_ON
7+ from homeassistant .core import callback
118
12- from .const import (
13- DOMAIN ,
14- FLAME_ICON ,
15- VALVE_CLOSED_ICON ,
16- VALVE_OPEN_ICON ,
17- WATER_ICON ,
18- )
19-
9+ from .const import DOMAIN , FLOW_OFF_ICON , FLOW_ON_ICON , IDLE_ICON , FLAME_ICON
2010from .sensor import SmileSensor
2111
22- BINARY_SENSOR_LIST = [
23- "dhw_state" ,
24- "slave_boiler_state" ,
25- "valve_position" ,
26- ]
12+ BINARY_SENSOR_MAP = {
13+ "dhw_state" : ["Domestic Hot Water State" , None ],
14+ "slave_boiler_state" : ["Secondary Heater Device State" , None ],
15+ }
2716
2817_LOGGER = logging .getLogger (__name__ )
2918
@@ -33,59 +22,45 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
3322 api = hass .data [DOMAIN ][config_entry .entry_id ]["api" ]
3423 coordinator = hass .data [DOMAIN ][config_entry .entry_id ]["coordinator" ]
3524
36- devices = []
37- binary_sensor_classes = [
38- "heater_central" ,
39- "thermo_sensor" ,
40- "thermostatic_radiator_valve" ,
41- ]
25+ entities = []
26+
4227 all_devices = api .get_all_devices ()
43- for dev_id , device in all_devices .items ():
44- if device ["class" ] in binary_sensor_classes :
45- _LOGGER .debug ("Plugwise device_class %s found" , device ["class" ])
28+ for dev_id , device_properties in all_devices .items ():
29+ if device_properties ["class" ] == "heater_central" :
30+ _LOGGER .debug ("Plugwise device_class %s found" , device_properties ["class" ])
4631 data = api .get_device_data (dev_id )
47- for binary_sensor in BINARY_SENSOR_LIST :
32+ for binary_sensor , b_s_type in BINARY_SENSOR_MAP . items () :
4833 _LOGGER .debug ("Binary_sensor: %s" , binary_sensor )
4934 if binary_sensor in data :
50- _LOGGER .debug ("Plugwise binary_sensor Dev %s" , device ["name" ])
51- devices .append (
35+ _LOGGER .debug (
36+ "Plugwise binary_sensor Dev %s" , device_properties ["name" ]
37+ )
38+ entities .append (
5239 PwBinarySensor (
5340 api ,
5441 coordinator ,
55- device ["name" ],
42+ device_properties ["name" ],
5643 binary_sensor ,
5744 dev_id ,
58- device ["class" ],
45+ device_properties ["class" ],
5946 )
6047 )
61- _LOGGER .info ("Added binary_sensor.%s" , device ["name" ])
48+ _LOGGER .info ("Added binary_sensor.%s" , device_properties ["name" ])
6249
63- async_add_entities (devices , True )
50+ async_add_entities (entities , True )
6451
6552
6653class PwBinarySensor (SmileSensor , BinarySensorEntity ):
6754 """Representation of a Plugwise binary_sensor."""
6855
6956 def __init__ (self , api , coordinator , name , binary_sensor , dev_id , model ):
7057 """Set up the Plugwise API."""
71- super ().__init__ (api , coordinator )
58+ super ().__init__ (api , coordinator , name , dev_id , binary_sensor )
7259
73- self ._api = api
74- self ._dev_id = dev_id
75- self ._entity_name = name
7660 self ._binary_sensor = binary_sensor
77- self ._is_on = False
78- self ._state = None
79- self ._dev_class = None
80-
81- if self ._dev_id == self ._api .heater_id :
82- self ._entity_name = f"Auxiliary"
83-
84- if self ._dev_id == self ._api .gateway_id :
85- self ._entity_name = f"Smile { self ._name } "
8661
87- bsensorname = binary_sensor . replace ( "_" , " " ). title ()
88- self ._name = f" { self . _entity_name } { bsensorname } "
62+ self . _is_on = False
63+ self ._icon = None
8964
9065 self ._unique_id = f"bs-{ dev_id } -{ self ._entity_name } -{ binary_sensor } "
9166
@@ -94,32 +69,35 @@ def is_on(self):
9469 """Return true if the binary sensor is on."""
9570 return self .is_on
9671
97- def _process_data (self ):
72+ @property
73+ def icon (self ):
74+ """Return the icon to use in the frontend."""
75+ return self ._icon
76+
77+ @callback
78+ def _async_process_data (self ):
9879 """Update the entity."""
9980 _LOGGER .debug ("Update binary_sensor called" )
10081 data = self ._api .get_device_data (self ._dev_id )
10182
10283 if not data :
10384 _LOGGER .error ("Received no data for device %s." , self ._binary_sensor )
104- else :
105- if self ._binary_sensor in data :
106- self ._state = STATE_OFF
107-
108- if isinstance (data [self ._binary_sensor ], float ):
109- self ._is_on = data [self ._binary_sensor ] == 1.0
110- self ._is_on = data [self ._binary_sensor ]
111-
112- if self ._is_on :
113- self ._state = STATE_ON
114-
85+ self .async_write_ha_state ()
86+ return
87+
88+ if self ._binary_sensor in data :
89+ self ._is_on = data [self ._binary_sensor ]
90+
91+ self ._state = STATE_OFF
92+ if self ._binary_sensor == "dhw_state" :
93+ self ._icon = FLOW_OFF_ICON
94+ if self ._binary_sensor == "slave_boiler_state" :
95+ self ._icon = IDLE_ICON
96+ if self ._is_on :
97+ self ._state = STATE_ON
11598 if self ._binary_sensor == "dhw_state" :
116- self ._icon = WATER_ICON
99+ self ._icon = FLOW_ON_ICON
117100 if self ._binary_sensor == "slave_boiler_state" :
118101 self ._icon = FLAME_ICON
119- if self ._binary_sensor == "valve_position" :
120- self ._dev_class = DEVICE_CLASS_OPENING
121- self ._icon = VALVE_CLOSED_ICON
122- if self ._is_on :
123- self ._icon = VALVE_OPEN_ICON
124102
125- self .async_write_ha_state ()
103+ self .async_write_ha_state ()
0 commit comments