From b812306bd71b491741ccce0ed26dd4fa95def2d6 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Fri, 18 Oct 2024 08:01:32 +0200 Subject: [PATCH] Use shorthand attribute in threshold binary sensor (#128612) Small refactor threshold --- .../components/threshold/binary_sensor.py | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/threshold/binary_sensor.py b/homeassistant/components/threshold/binary_sensor.py index 9440e251586..5f1639ff2e1 100644 --- a/homeassistant/components/threshold/binary_sensor.py +++ b/homeassistant/components/threshold/binary_sensor.py @@ -177,7 +177,6 @@ class ThresholdSensor(BinarySensorEntity): 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: @@ -229,11 +228,6 @@ class ThresholdSensor(BinarySensorEntity): ) _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.""" @@ -261,53 +255,53 @@ class ThresholdSensor(BinarySensorEntity): 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