Opower: Fix unavailable "start date" and "end date" sensors (#138694)

avoid passing string into date device class
This commit is contained in:
Saswat Padhi 2025-02-20 07:42:09 +00:00 committed by Franck Nijhof
parent 2b7543aca2
commit 179ba8309d
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3

View File

@ -4,6 +4,7 @@ from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from datetime import date
from opower import Forecast, MeterType, UnitOfMeasure
@ -29,7 +30,7 @@ from .coordinator import OpowerCoordinator
class OpowerEntityDescription(SensorEntityDescription):
"""Class describing Opower sensors entities."""
value_fn: Callable[[Forecast], str | float]
value_fn: Callable[[Forecast], str | float | date]
# suggested_display_precision=0 for all sensors since
@ -97,7 +98,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
device_class=SensorDeviceClass.DATE,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda data: str(data.start_date),
value_fn=lambda data: data.start_date,
),
OpowerEntityDescription(
key="elec_end_date",
@ -105,7 +106,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
device_class=SensorDeviceClass.DATE,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda data: str(data.end_date),
value_fn=lambda data: data.end_date,
),
)
GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
@ -169,7 +170,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
device_class=SensorDeviceClass.DATE,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda data: str(data.start_date),
value_fn=lambda data: data.start_date,
),
OpowerEntityDescription(
key="gas_end_date",
@ -177,7 +178,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
device_class=SensorDeviceClass.DATE,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda data: str(data.end_date),
value_fn=lambda data: data.end_date,
),
)
@ -247,7 +248,7 @@ class OpowerSensor(CoordinatorEntity[OpowerCoordinator], SensorEntity):
self.utility_account_id = utility_account_id
@property
def native_value(self) -> StateType:
def native_value(self) -> StateType | date:
"""Return the state."""
if self.coordinator.data is not None:
return self.entity_description.value_fn(