Skip to content

Commit

Permalink
Use shorthand attribute in threshold binary sensor (home-assistant#12…
Browse files Browse the repository at this point in the history
…8612)

Small refactor threshold
  • Loading branch information
gjohansson-ST authored Oct 18, 2024
1 parent 0e667df commit b812306
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions homeassistant/components/threshold/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def __init__(
self._hysteresis: float = hysteresis
self._attr_device_class = device_class
self._state_position = POSITION_UNKNOWN
self._state: bool | None = None
self.sensor_value: float | None = None

async def async_added_to_hass(self) -> None:
Expand Down Expand Up @@ -229,11 +228,6 @@ def async_threshold_sensor_state_listener(
)
_update_sensor_state()

@property
def is_on(self) -> bool | None:
"""Return true if sensor is on."""
return self._state

@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of the sensor."""
Expand Down Expand Up @@ -261,53 +255,53 @@ def above(sensor_value: float, threshold: float) -> bool:

if self.sensor_value is None:
self._state_position = POSITION_UNKNOWN
self._state = None
self._attr_is_on = None
return

if self.threshold_type == TYPE_LOWER:
if self._state is None:
self._state = False
if self._attr_is_on is None:
self._attr_is_on = False
self._state_position = POSITION_ABOVE

if below(self.sensor_value, self._threshold_lower):
self._state_position = POSITION_BELOW
self._state = True
self._attr_is_on = True
elif above(self.sensor_value, self._threshold_lower):
self._state_position = POSITION_ABOVE
self._state = False
self._attr_is_on = False
return

if self.threshold_type == TYPE_UPPER:
assert self._threshold_upper is not None

if self._state is None:
self._state = False
if self._attr_is_on is None:
self._attr_is_on = False
self._state_position = POSITION_BELOW

if above(self.sensor_value, self._threshold_upper):
self._state_position = POSITION_ABOVE
self._state = True
self._attr_is_on = True
elif below(self.sensor_value, self._threshold_upper):
self._state_position = POSITION_BELOW
self._state = False
self._attr_is_on = False
return

if self.threshold_type == TYPE_RANGE:
if self._state is None:
self._state = True
if self._attr_is_on is None:
self._attr_is_on = True
self._state_position = POSITION_IN_RANGE

if below(self.sensor_value, self._threshold_lower):
self._state_position = POSITION_BELOW
self._state = False
self._attr_is_on = False
if above(self.sensor_value, self._threshold_upper):
self._state_position = POSITION_ABOVE
self._state = False
self._attr_is_on = False
elif above(self.sensor_value, self._threshold_lower) and below(
self.sensor_value, self._threshold_upper
):
self._state_position = POSITION_IN_RANGE
self._state = True
self._attr_is_on = True
return

@callback
Expand Down

0 comments on commit b812306

Please sign in to comment.