66 SensorEntity
77)
88from homeassistant .helpers .update_coordinator import CoordinatorEntity
9- from homeassistant .const import UnitOfPower , UnitOfEnergy , UnitOfElectricCurrent
9+ from homeassistant .const import UnitOfPower , UnitOfEnergy , UnitOfElectricCurrent , UnitOfElectricPotential
1010from homeassistant .core import HomeAssistant , callback
1111from homeassistant .helpers .entity import generate_entity_id
1212from homeassistant .util .dt import (utcnow )
@@ -30,9 +30,11 @@ async def async_setup_entry(
3030
3131 sensors = [PowerDrawSensor (coordinator , hass , client ),
3232 CurrentDrawSensor (coordinator , hass , client ),
33+ VoltageSensor (coordinator , hass , client ),
3334 CTSensor (adv_coordinator , hass , client ),
3435 EnergyUsageSensor (stats_coordinator , hass , client ),
35- NextSlotSensor (coordinator , hass , client )]
36+ NextSlotEndSensor (coordinator , hass , client ),
37+ NextSlotStartSensor (coordinator , hass , client )]
3638
3739 async_add_entities (sensors , update_before_add = True )
3840
@@ -121,15 +123,57 @@ def native_value(self):
121123 return 0
122124
123125
124- class CTSensor (CoordinatorEntity [OhmeChargeSessionsCoordinator ], SensorEntity ):
126+ class VoltageSensor (CoordinatorEntity [OhmeChargeSessionsCoordinator ], SensorEntity ):
127+ """Sensor for EVSE voltage."""
128+ _attr_name = "Voltage"
129+ _attr_device_class = SensorDeviceClass .VOLTAGE
130+ _attr_native_unit_of_measurement = UnitOfElectricPotential .VOLT
131+
132+ def __init__ (
133+ self ,
134+ coordinator : OhmeChargeSessionsCoordinator ,
135+ hass : HomeAssistant ,
136+ client ):
137+ super ().__init__ (coordinator = coordinator )
138+
139+ self ._state = None
140+ self ._attributes = {}
141+ self ._last_updated = None
142+ self ._client = client
143+
144+ self .entity_id = generate_entity_id (
145+ "sensor.{}" , "ohme_voltage" , hass = hass )
146+
147+ self ._attr_device_info = hass .data [DOMAIN ][DATA_CLIENT ].get_device_info (
148+ )
149+
150+ @property
151+ def unique_id (self ) -> str :
152+ """Return the unique ID of the sensor."""
153+ return self ._client .get_unique_id ("voltage" )
154+
155+ @property
156+ def icon (self ):
157+ """Icon of the sensor."""
158+ return "mdi:sine-wave"
159+
160+ @property
161+ def native_value (self ):
162+ """Get value from data returned from API by coordinator"""
163+ if self .coordinator .data and self .coordinator .data ['power' ]:
164+ return self .coordinator .data ['power' ]['volt' ]
165+ return None
166+
167+
168+ class CTSensor (CoordinatorEntity [OhmeAdvancedSettingsCoordinator ], SensorEntity ):
125169 """Sensor for car power draw."""
126170 _attr_name = "CT Reading"
127171 _attr_device_class = SensorDeviceClass .CURRENT
128172 _attr_native_unit_of_measurement = UnitOfElectricCurrent .AMPERE
129173
130174 def __init__ (
131175 self ,
132- coordinator : OhmeChargeSessionsCoordinator ,
176+ coordinator : OhmeAdvancedSettingsCoordinator ,
133177 hass : HomeAssistant ,
134178 client ):
135179 super ().__init__ (coordinator = coordinator )
@@ -171,7 +215,7 @@ class EnergyUsageSensor(CoordinatorEntity[OhmeStatisticsCoordinator], SensorEnti
171215
172216 def __init__ (
173217 self ,
174- coordinator : OhmeChargeSessionsCoordinator ,
218+ coordinator : OhmeStatisticsCoordinator ,
175219 hass : HomeAssistant ,
176220 client ):
177221 super ().__init__ (coordinator = coordinator )
@@ -206,9 +250,9 @@ def native_value(self):
206250 return None
207251
208252
209- class NextSlotSensor (CoordinatorEntity [OhmeStatisticsCoordinator ], SensorEntity ):
210- """Sensor for next smart charge slot."""
211- _attr_name = "Next Smart Charge Slot"
253+ class NextSlotStartSensor (CoordinatorEntity [OhmeChargeSessionsCoordinator ], SensorEntity ):
254+ """Sensor for next smart charge slot start time ."""
255+ _attr_name = "Next Charge Slot Start "
212256 _attr_device_class = SensorDeviceClass .TIMESTAMP
213257
214258 def __init__ (
@@ -234,6 +278,58 @@ def unique_id(self) -> str:
234278 """Return the unique ID of the sensor."""
235279 return self ._client .get_unique_id ("next_slot" )
236280
281+ @property
282+ def icon (self ):
283+ """Icon of the sensor."""
284+ return "mdi:clock-star-four-points"
285+
286+ @property
287+ def native_value (self ):
288+ """Return pre-calculated state."""
289+ return self ._state
290+
291+ @callback
292+ def _handle_coordinator_update (self ) -> None :
293+ """Calculate next timeslot. This is a bit slow so we only update on coordinator data update."""
294+ if self .coordinator .data is None or self .coordinator .data ["mode" ] == "DISCONNECTED" :
295+ self ._state = None
296+ else :
297+ self ._state = charge_graph_next_slot (
298+ self .coordinator .data ['startTime' ], self .coordinator .data ['chargeGraph' ]['points' ])['start' ]
299+
300+ self ._last_updated = utcnow ()
301+
302+ self .async_write_ha_state ()
303+
304+
305+ class NextSlotEndSensor (CoordinatorEntity [OhmeChargeSessionsCoordinator ], SensorEntity ):
306+ """Sensor for next smart charge slot end time."""
307+ _attr_name = "Next Charge Slot End"
308+ _attr_device_class = SensorDeviceClass .TIMESTAMP
309+
310+ def __init__ (
311+ self ,
312+ coordinator : OhmeChargeSessionsCoordinator ,
313+ hass : HomeAssistant ,
314+ client ):
315+ super ().__init__ (coordinator = coordinator )
316+
317+ self ._state = None
318+ self ._attributes = {}
319+ self ._last_updated = None
320+ self ._client = client
321+
322+ self .entity_id = generate_entity_id (
323+ "sensor.{}" , "ohme_next_slot_end" , hass = hass )
324+
325+ self ._attr_device_info = hass .data [DOMAIN ][DATA_CLIENT ].get_device_info (
326+ )
327+
328+ @property
329+ def unique_id (self ) -> str :
330+ """Return the unique ID of the sensor."""
331+ return self ._client .get_unique_id ("next_slot_end" )
332+
237333 @property
238334 def icon (self ):
239335 """Icon of the sensor."""
@@ -251,7 +347,7 @@ def _handle_coordinator_update(self) -> None:
251347 self ._state = None
252348 else :
253349 self ._state = charge_graph_next_slot (
254- self .coordinator .data ['startTime' ], self .coordinator .data ['chargeGraph' ]['points' ])
350+ self .coordinator .data ['startTime' ], self .coordinator .data ['chargeGraph' ]['points' ])[ 'end' ]
255351
256352 self ._last_updated = utcnow ()
257353
0 commit comments