11import logging
22from datetime import timedelta
33from homeassistant .const import UnitOfPower , UnitOfTemperature , PERCENTAGE , STATE_OFF
4- from homeassistant .components .sensor import SensorEntity
5- from homeassistant .components .sensor import SensorDeviceClass
4+ from homeassistant .components .sensor import SensorDeviceClass , SensorEntity
65from homeassistant .components .integration .sensor import IntegrationSensor
7- from homeassistant .components .binary_sensor import BinarySensorDeviceClass
8- from homeassistant .components .binary_sensor import BinarySensorEntity
6+ from homeassistant .components .binary_sensor import BinarySensorDeviceClass , BinarySensorEntity
97from .const import DOMAIN
108from .tank import Tank
11- from homeassistant .helpers .update_coordinator import (
12- CoordinatorEntity ,
13- DataUpdateCoordinator ,
14- )
9+ from .mixergy_entity import MixergyEntityBase
1510
1611_LOGGER = logging .getLogger (__name__ )
1712
1813async def async_setup_entry (hass , config_entry , async_add_entities ):
1914 _LOGGER .info ("Setting up entry based on user config" )
2015
21- tank = hass .data [DOMAIN ][config_entry .entry_id ]
22-
23- async def async_update_data ():
24- _LOGGER .info ("Fetching data from Mixergy..." )
25- await tank .fetch_data ()
26-
27- # Create a coordinator to fetch data from the Mixergy API.
28- coordinator = DataUpdateCoordinator (hass , _LOGGER , name = "sensor" , update_method = async_update_data , update_interval = timedelta (seconds = 30 ))
29-
30- await coordinator .async_config_entry_first_refresh ()
16+ entry = hass .data [DOMAIN ][config_entry .entry_id ]
17+ tank = entry ["tank" ]
18+ coordinator = entry ["coordinator" ]
3119
3220 new_entities = []
3321
@@ -48,66 +36,18 @@ async def async_update_data():
4836 new_entities .append (PVEnergySensor (tank ))
4937 new_entities .append (ClampPowerSensor (coordinator , tank ))
5038 new_entities .append (IsChargingSensor (coordinator , tank ))
51-
52- async_add_entities (new_entities )
5339
54- class SensorBase ( CoordinatorEntity , SensorEntity ):
40+ async_add_entities ( new_entities )
5541
56- should_poll = True
42+ class SensorBase ( MixergyEntityBase , SensorEntity ):
5743
5844 def __init__ (self , coordinator , tank :Tank ):
59- super ().__init__ (coordinator )
60- self ._tank = tank
61-
62- @property
63- def device_info (self ):
64- return {
65- "identifiers" : {(DOMAIN , self ._tank .serial_number )},
66- "manufacturer" :"Mixergy Ltd" ,
67- "name" :"Mixergy Tank" ,
68- "suggested_area" :"garage" ,
69- "model" :self ._tank .modelCode ,
70- "sw_version" :self ._tank .firmwareVersion
71- }
72-
73- @property
74- def available (self ) -> bool :
75- return self ._tank .online
76-
77- async def async_added_to_hass (self ):
78- self ._tank .register_callback (self .async_write_ha_state )
79-
80- async def async_will_remove_from_hass (self ):
81- self ._tank .remove_callback (self .async_write_ha_state )
82-
83- class BinarySensorBase (CoordinatorEntity ,BinarySensorEntity ):
45+ super ().__init__ (coordinator , tank )
8446
85- should_poll = True
47+ class BinarySensorBase ( MixergyEntityBase , BinarySensorEntity ):
8648
8749 def __init__ (self , coordinator , tank :Tank ):
88- super ().__init__ (coordinator )
89- self ._tank = tank
90-
91- @property
92- def device_info (self ):
93- return {
94- "identifiers" : {(DOMAIN , self ._tank .serial_number )},
95- "manufacturer" :"Mixergy Ltd" ,
96- "name" :"Mixergy Tank" ,
97- "suggested_area" :"garage" ,
98- "model" :self ._tank .modelCode ,
99- "sw_version" :self ._tank .firmwareVersion
100- }
101-
102- @property
103- def available (self ) -> bool :
104- return self ._tank .online
105-
106- async def async_added_to_hass (self ):
107- self ._tank .register_callback (self .async_write_ha_state )
108-
109- async def async_will_remove_from_hass (self ):
110- self ._tank .remove_callback (self .async_write_ha_state )
50+ super ().__init__ (coordinator , tank )
11151
11252class ChargeSensor (SensorBase ):
11353
@@ -133,7 +73,7 @@ def icon(self):
13373 @property
13474 def name (self ):
13575 return f"Current Charge"
136-
76+
13777class TargetChargeSensor (SensorBase ):
13878
13979 def __init__ (self , coordinator , tank :Tank ):
@@ -205,7 +145,7 @@ def unit_of_measurement(self):
205145 @property
206146 def name (self ):
207147 return f"Coldest Water Temperature"
208-
148+
209149class TargetTemperatureSensor (SensorBase ):
210150
211151 device_class = SensorDeviceClass .TEMPERATURE
@@ -335,7 +275,7 @@ def icon(self):
335275 @property
336276 def name (self ):
337277 return f"Low Hot Water"
338-
278+
339279class IsChargingSensor (BinarySensorBase ):
340280
341281 def __init__ (self , coordinator , tank :Tank ):
@@ -461,7 +401,7 @@ def state(self):
461401
462402 @property
463403 def unit_of_measurement (self ):
464- return POWER_WATT
404+ return UnitOfPower . WATT
465405
466406 @property
467407 def name (self ):
0 commit comments