Handle missing attributes in meater objects (#76072)

This commit is contained in:
Erik Montnemery
2022-08-02 14:13:07 +02:00
committed by GitHub
parent d69d7a8761
commit 404d530b5f

View File

@@ -43,14 +43,18 @@ class MeaterSensorEntityDescription(
def _elapsed_time_to_timestamp(probe: MeaterProbe) -> datetime | None:
"""Convert elapsed time to timestamp."""
if not probe.cook:
if not probe.cook or not hasattr(probe.cook, "time_elapsed"):
return None
return dt_util.utcnow() - timedelta(seconds=probe.cook.time_elapsed)
def _remaining_time_to_timestamp(probe: MeaterProbe) -> datetime | None:
"""Convert remaining time to timestamp."""
if not probe.cook or probe.cook.time_remaining < 0:
if (
not probe.cook
or not hasattr(probe.cook, "time_remaining")
or probe.cook.time_remaining < 0
):
return None
return dt_util.utcnow() + timedelta(seconds=probe.cook.time_remaining)
@@ -99,7 +103,9 @@ SENSOR_TYPES = (
native_unit_of_measurement=TEMP_CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
available=lambda probe: probe is not None and probe.cook is not None,
value=lambda probe: probe.cook.target_temperature if probe.cook else None,
value=lambda probe: probe.cook.target_temperature
if probe.cook and hasattr(probe.cook, "target_temperature")
else None,
),
# Peak temperature
MeaterSensorEntityDescription(
@@ -109,7 +115,9 @@ SENSOR_TYPES = (
native_unit_of_measurement=TEMP_CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
available=lambda probe: probe is not None and probe.cook is not None,
value=lambda probe: probe.cook.peak_temperature if probe.cook else None,
value=lambda probe: probe.cook.peak_temperature
if probe.cook and hasattr(probe.cook, "peak_temperature")
else None,
),
# 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.