Refactor Meater availability (#146956)

* Refactor Meater availability

* Fix

* Fix
This commit is contained in:
Joost Lekkerkerker 2025-06-25 09:23:27 +02:00 committed by GitHub
parent 066e840e06
commit 51fb1ab8b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -42,8 +42,8 @@ COOK_STATES = {
class MeaterSensorEntityDescription(SensorEntityDescription): class MeaterSensorEntityDescription(SensorEntityDescription):
"""Describes meater sensor entity.""" """Describes meater sensor entity."""
available: Callable[[MeaterProbe | None], bool]
value: Callable[[MeaterProbe], datetime | float | str | None] value: Callable[[MeaterProbe], datetime | float | str | None]
unavailable_when_not_cooking: bool = False
def _elapsed_time_to_timestamp(probe: MeaterProbe) -> datetime | None: def _elapsed_time_to_timestamp(probe: MeaterProbe) -> datetime | None:
@ -72,7 +72,6 @@ SENSOR_TYPES = (
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
available=lambda probe: probe is not None,
value=lambda probe: probe.ambient_temperature, value=lambda probe: probe.ambient_temperature,
), ),
# Internal temperature (probe tip) # Internal temperature (probe tip)
@ -82,20 +81,19 @@ SENSOR_TYPES = (
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
available=lambda probe: probe is not None,
value=lambda probe: probe.internal_temperature, value=lambda probe: probe.internal_temperature,
), ),
# Name of selected meat in user language or user given custom name # Name of selected meat in user language or user given custom name
MeaterSensorEntityDescription( MeaterSensorEntityDescription(
key="cook_name", key="cook_name",
translation_key="cook_name", translation_key="cook_name",
available=lambda probe: probe is not None and probe.cook is not None, unavailable_when_not_cooking=True,
value=lambda probe: probe.cook.name if probe.cook else None, value=lambda probe: probe.cook.name if probe.cook else None,
), ),
MeaterSensorEntityDescription( MeaterSensorEntityDescription(
key="cook_state", key="cook_state",
translation_key="cook_state", translation_key="cook_state",
available=lambda probe: probe is not None and probe.cook is not None, unavailable_when_not_cooking=True,
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=list(COOK_STATES.values()), options=list(COOK_STATES.values()),
value=lambda probe: COOK_STATES.get(probe.cook.state) if probe.cook else None, value=lambda probe: COOK_STATES.get(probe.cook.state) if probe.cook else None,
@ -107,10 +105,12 @@ SENSOR_TYPES = (
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
available=lambda probe: probe is not None and probe.cook is not None, unavailable_when_not_cooking=True,
value=lambda probe: probe.cook.target_temperature value=(
if probe.cook and hasattr(probe.cook, "target_temperature") lambda probe: probe.cook.target_temperature
else None, if probe.cook and hasattr(probe.cook, "target_temperature")
else None
),
), ),
# Peak temperature # Peak temperature
MeaterSensorEntityDescription( MeaterSensorEntityDescription(
@ -119,10 +119,12 @@ SENSOR_TYPES = (
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
available=lambda probe: probe is not None and probe.cook is not None, unavailable_when_not_cooking=True,
value=lambda probe: probe.cook.peak_temperature value=(
if probe.cook and hasattr(probe.cook, "peak_temperature") lambda probe: probe.cook.peak_temperature
else None, if probe.cook and hasattr(probe.cook, "peak_temperature")
else None
),
), ),
# Remaining time in seconds. When unknown/calculating default is used. Default: -1 # Remaining time in seconds. When unknown/calculating default is used. Default: -1
# Exposed as a TIMESTAMP sensor where the timestamp is current time + remaining time. # Exposed as a TIMESTAMP sensor where the timestamp is current time + remaining time.
@ -130,7 +132,7 @@ SENSOR_TYPES = (
key="cook_time_remaining", key="cook_time_remaining",
translation_key="cook_time_remaining", translation_key="cook_time_remaining",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
available=lambda probe: probe is not None and probe.cook is not None, unavailable_when_not_cooking=True,
value=_remaining_time_to_timestamp, value=_remaining_time_to_timestamp,
), ),
# Time since the start of cook in seconds. Default: 0. Exposed as a TIMESTAMP sensor # Time since the start of cook in seconds. Default: 0. Exposed as a TIMESTAMP sensor
@ -139,7 +141,7 @@ SENSOR_TYPES = (
key="cook_time_elapsed", key="cook_time_elapsed",
translation_key="cook_time_elapsed", translation_key="cook_time_elapsed",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
available=lambda probe: probe is not None and probe.cook is not None, unavailable_when_not_cooking=True,
value=_elapsed_time_to_timestamp, value=_elapsed_time_to_timestamp,
), ),
) )
@ -192,7 +194,10 @@ class MeaterProbeTemperature(SensorEntity, CoordinatorEntity[MeaterCoordinator])
entity_description: MeaterSensorEntityDescription entity_description: MeaterSensorEntityDescription
def __init__( def __init__(
self, coordinator, device_id, description: MeaterSensorEntityDescription self,
coordinator: MeaterCoordinator,
device_id: str,
description: MeaterSensorEntityDescription,
) -> None: ) -> None:
"""Initialise the sensor.""" """Initialise the sensor."""
super().__init__(coordinator) super().__init__(coordinator)
@ -211,20 +216,24 @@ class MeaterProbeTemperature(SensorEntity, CoordinatorEntity[MeaterCoordinator])
self.entity_description = description self.entity_description = description
@property @property
def native_value(self): def probe(self) -> MeaterProbe:
"""Return the temperature of the probe.""" """Return the probe."""
if not (device := self.coordinator.data.get(self.device_id)): return self.coordinator.data[self.device_id]
return None
return self.entity_description.value(device) @property
def native_value(self) -> datetime | float | str | None:
"""Return the temperature of the probe."""
return self.entity_description.value(self.probe)
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return if entity is available.""" """Return if entity is available."""
# See if the device was returned from the API. If not, it's offline # See if the device was returned from the API. If not, it's offline
return ( return (
self.coordinator.last_update_success super().available
and self.entity_description.available( and self.device_id in self.coordinator.data
self.coordinator.data.get(self.device_id) and (
not self.entity_description.unavailable_when_not_cooking
or self.probe.cook is not None
) )
) )