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