diff --git a/homeassistant/components/blue_current/sensor.py b/homeassistant/components/blue_current/sensor.py index b544b69d2ff..4c590544984 100644 --- a/homeassistant/components/blue_current/sensor.py +++ b/homeassistant/components/blue_current/sensor.py @@ -23,8 +23,6 @@ from . import Connector from .const import DOMAIN from .entity import BlueCurrentEntity, ChargepointEntity -TIMESTAMP_KEYS = ("start_datetime", "stop_datetime", "offline_since") - SENSORS = ( SensorEntityDescription( key="actual_v1", @@ -102,21 +100,6 @@ SENSORS = ( translation_key="actual_kwh", state_class=SensorStateClass.TOTAL_INCREASING, ), - SensorEntityDescription( - key="start_datetime", - device_class=SensorDeviceClass.TIMESTAMP, - translation_key="start_datetime", - ), - SensorEntityDescription( - key="stop_datetime", - device_class=SensorDeviceClass.TIMESTAMP, - translation_key="stop_datetime", - ), - SensorEntityDescription( - key="offline_since", - device_class=SensorDeviceClass.TIMESTAMP, - translation_key="offline_since", - ), SensorEntityDescription( key="total_cost", native_unit_of_measurement=CURRENCY_EURO, @@ -168,6 +151,21 @@ SENSORS = ( ), ) +TIMESTAMP_SENSORS = ( + SensorEntityDescription( + key="start_datetime", + translation_key="start_datetime", + ), + SensorEntityDescription( + key="stop_datetime", + translation_key="stop_datetime", + ), + SensorEntityDescription( + key="offline_since", + translation_key="offline_since", + ), +) + GRID_SENSORS = ( SensorEntityDescription( key="grid_actual_p1", @@ -223,6 +221,14 @@ async def async_setup_entry( for sensor in SENSORS ] + sensor_list.extend( + [ + ChargePointTimestampSensor(connector, sensor, evse_id) + for evse_id in connector.charge_points + for sensor in TIMESTAMP_SENSORS + ] + ) + sensor_list.extend(GridSensor(connector, sensor) for sensor in GRID_SENSORS) async_add_entities(sensor_list) @@ -251,17 +257,31 @@ class ChargePointSensor(ChargepointEntity, SensorEntity): new_value = self.connector.charge_points[self.evse_id].get(self.key) if new_value is not None: - if self.key in TIMESTAMP_KEYS and not ( - self._attr_native_value is None or self._attr_native_value < new_value - ): - return self.has_value = True self._attr_native_value = new_value - elif self.key not in TIMESTAMP_KEYS: + else: self.has_value = False +class ChargePointTimestampSensor(ChargePointSensor): + """Define a timestamp sensor.""" + + _attr_device_class = SensorDeviceClass.TIMESTAMP + + @callback + def update_from_latest_data(self) -> None: + """Update the sensor from the latest data.""" + new_value = self.connector.charge_points[self.evse_id].get(self.key) + + # only update if the new_value is a newer timestamp. + if new_value is not None and ( + self.has_value is False or self._attr_native_value < new_value + ): + self.has_value = True + self._attr_native_value = new_value + + class GridSensor(BlueCurrentEntity, SensorEntity): """Define a grid sensor."""