Skip to content

Commit

Permalink
Small cleanup mold_indicator (home-assistant#129736)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST authored Nov 3, 2024
1 parent ed582fa commit d671d48
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions homeassistant/components/mold_indicator/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
CONF_NAME,
CONF_UNIQUE_ID,
PERCENTAGE,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
UnitOfTemperature,
)
Expand Down Expand Up @@ -310,16 +311,14 @@ def _update_temp_sensor(state: State) -> float | None:
_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,
state.state,
)
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",
Expand All @@ -329,12 +328,10 @@ def _update_temp_sensor(state: State) -> float | None:
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,
Expand All @@ -351,7 +348,7 @@ def _update_hum_sensor(state: State) -> float | None:
_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,
Expand All @@ -369,19 +366,18 @@ def _update_hum_sensor(state: State) -> float | None:

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

Expand Down

0 comments on commit d671d48

Please sign in to comment.