88)
99from homeassistant .const import (
1010 UnitOfPower ,
11- UnitOfEnergy
11+ UnitOfEnergy ,
12+ PERCENTAGE
1213)
1314from homeassistant .core import HomeAssistant
1415from homeassistant .helpers .entity_platform import AddEntitiesCallback
@@ -29,6 +30,8 @@ async def async_setup_entry(hass, entry, async_add_entities):
2930 new_devices = []
3031 sites = await asyncio .to_thread (lambda : client .get_sites ())
3132 for site in sites :
33+ new_devices .append (ComwattAutoProductionRateSensor (entry , site ))
34+
3235 devices = await asyncio .to_thread (lambda : client .get_devices (site ['id' ]))
3336 for device in devices :
3437 if 'id' in device :
@@ -50,9 +53,11 @@ async def async_setup_entry(hass, entry, async_add_entities):
5053class ComwattSensor (SensorEntity ):
5154 @property
5255 def device_info (self ) -> DeviceInfo :
53- """Return te devce info."""
56+ """Return te device info."""
5457 if 'deviceKind' in self ._device and 'code' in self ._device ['deviceKind' ]:
5558 model = self ._device ['deviceKind' ]['code' ]
59+ elif 'siteKind' in self ._device :
60+ model = self ._device ['siteKind' ]
5661 else :
5762 model = None
5863
@@ -65,6 +70,35 @@ def device_info(self) -> DeviceInfo:
6570 model = model
6671 )
6772
73+ class ComwattAutoProductionRateSensor (ComwattSensor ):
74+ """Representation of an Auto Production Rate Sensor."""
75+ _attr_native_unit_of_measurement = PERCENTAGE
76+ _attr_state_class = SensorStateClass .MEASUREMENT
77+
78+ def __init__ (self , entry , device ):
79+ self ._device = device
80+ self ._username = entry .data ["username" ]
81+ self ._password = entry .data ["password" ]
82+ self ._attr_unique_id = f"site_{ self ._device ['id' ]} _auto_production_rate"
83+ self ._attr_name = f"{ self ._device ['name' ]} Auto Production Rate"
84+
85+ def update (self ) -> None :
86+ """Fetch new state data for the sensor."""
87+ client = ComwattClient ()
88+ client .session .cookies .update (self .hass .data [DOMAIN ]["cookies" ])
89+ try :
90+ time_series_data = client .get_site_networks_ts_time_ago (self ._device ["id" ], "FLOW" , "NONE" , None , "HOUR" , 1 )
91+ self ._attr_native_value = auto_production_rate
92+ except Exception :
93+ client .authenticate (self ._username , self ._password )
94+ self .hass .data [DOMAIN ]["cookies" ] = client .session .cookies .get_dict ()
95+ time_series_data = client .get_site_networks_ts_time_ago (self ._device ["id" ], "FLOW" , "NONE" , None , "HOUR" , 1 )
96+
97+ # TODO: Update to the time of comwatt and not the current time
98+ if time_series_data ["autoproductionRates" ]:
99+ self ._attr_native_value = time_series_data ["autoproductionRates" ][- 1 ] * 100
100+
101+
68102class ComwattEnergySensor (ComwattSensor ):
69103 """Representation of a Sensor."""
70104
0 commit comments