Skip to content

Commit

Permalink
Separate Blue Current timestamp sensors (#111942)
Browse files Browse the repository at this point in the history
  • Loading branch information
Floris272 committed May 14, 2024
1 parent 03cce66 commit b4eeb00
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions homeassistant/components/blue_current/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
from .const import DOMAIN
from .entity import BlueCurrentEntity, ChargepointEntity

TIMESTAMP_KEYS = ("start_datetime", "stop_datetime", "offline_since")

SENSORS = (
SensorEntityDescription(
key="actual_v1",
Expand Down Expand Up @@ -102,21 +100,6 @@
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,
Expand Down Expand Up @@ -168,6 +151,21 @@
),
)

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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -251,17 +257,31 @@ def update_from_latest_data(self) -> None:
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."""

Expand Down

0 comments on commit b4eeb00

Please sign in to comment.