Small cleanup mold_indicator (#129736)

This commit is contained in:
G Johansson 2024-11-03 19:17:37 +01:00 committed by GitHub
parent ed582fae91
commit d671d48869
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,6 +22,7 @@ from homeassistant.const import (
CONF_NAME,
CONF_UNIQUE_ID,
PERCENTAGE,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
UnitOfTemperature,
)
@ -310,7 +311,7 @@ class MoldIndicator(SensorEntity):
_LOGGER.debug("Updating temp sensor with value %s", state.state)
# Return an error if the sensor change its state to Unknown.
if state.state == STATE_UNKNOWN:
if state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE):
_LOGGER.error(
"Unable to parse temperature sensor %s with state: %s",
state.entity_id,
@ -318,8 +319,6 @@ class MoldIndicator(SensorEntity):
)
return None
unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
if (temp := util.convert(state.state, float)) is None:
_LOGGER.error(
"Unable to parse temperature sensor %s with state: %s",
@ -329,12 +328,10 @@ class MoldIndicator(SensorEntity):
return None
# convert to celsius if necessary
if unit == UnitOfTemperature.FAHRENHEIT:
return TemperatureConverter.convert(
temp, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.CELSIUS
)
if unit == UnitOfTemperature.CELSIUS:
return temp
if (
unit := state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
) in UnitOfTemperature:
return TemperatureConverter.convert(temp, unit, UnitOfTemperature.CELSIUS)
_LOGGER.error(
"Temp sensor %s has unsupported unit: %s (allowed: %s, %s)",
state.entity_id,
@ -351,7 +348,7 @@ class MoldIndicator(SensorEntity):
_LOGGER.debug("Updating humidity sensor with value %s", state.state)
# Return an error if the sensor change its state to Unknown.
if state.state == STATE_UNKNOWN:
if state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE):
_LOGGER.error(
"Unable to parse humidity sensor %s, state: %s",
state.entity_id,
@ -369,19 +366,18 @@ class MoldIndicator(SensorEntity):
if (unit := state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)) != PERCENTAGE:
_LOGGER.error(
"Humidity sensor %s has unsupported unit: %s %s",
"Humidity sensor %s has unsupported unit: %s (allowed: %s)",
state.entity_id,
unit,
" (allowed: %)",
PERCENTAGE,
)
return None
if hum > 100 or hum < 0:
_LOGGER.error(
"Humidity sensor %s is out of range: %s %s",
"Humidity sensor %s is out of range: %s (allowed: 0-100)",
state.entity_id,
hum,
"(allowed: 0-100%)",
)
return None