Use shorthand attribute in threshold binary sensor (#128612)

Small refactor threshold
This commit is contained in:
G Johansson 2024-10-18 08:01:32 +02:00 committed by GitHub
parent 0e667dfe36
commit b812306bd7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -177,7 +177,6 @@ class ThresholdSensor(BinarySensorEntity):
self._hysteresis: float = hysteresis self._hysteresis: float = hysteresis
self._attr_device_class = device_class self._attr_device_class = device_class
self._state_position = POSITION_UNKNOWN self._state_position = POSITION_UNKNOWN
self._state: bool | None = None
self.sensor_value: float | None = None self.sensor_value: float | None = None
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
@ -229,11 +228,6 @@ class ThresholdSensor(BinarySensorEntity):
) )
_update_sensor_state() _update_sensor_state()
@property
def is_on(self) -> bool | None:
"""Return true if sensor is on."""
return self._state
@property @property
def extra_state_attributes(self) -> dict[str, Any]: def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
@ -261,53 +255,53 @@ class ThresholdSensor(BinarySensorEntity):
if self.sensor_value is None: if self.sensor_value is None:
self._state_position = POSITION_UNKNOWN self._state_position = POSITION_UNKNOWN
self._state = None self._attr_is_on = None
return return
if self.threshold_type == TYPE_LOWER: if self.threshold_type == TYPE_LOWER:
if self._state is None: if self._attr_is_on is None:
self._state = False self._attr_is_on = False
self._state_position = POSITION_ABOVE self._state_position = POSITION_ABOVE
if below(self.sensor_value, self._threshold_lower): if below(self.sensor_value, self._threshold_lower):
self._state_position = POSITION_BELOW self._state_position = POSITION_BELOW
self._state = True self._attr_is_on = True
elif above(self.sensor_value, self._threshold_lower): elif above(self.sensor_value, self._threshold_lower):
self._state_position = POSITION_ABOVE self._state_position = POSITION_ABOVE
self._state = False self._attr_is_on = False
return return
if self.threshold_type == TYPE_UPPER: if self.threshold_type == TYPE_UPPER:
assert self._threshold_upper is not None assert self._threshold_upper is not None
if self._state is None: if self._attr_is_on is None:
self._state = False self._attr_is_on = False
self._state_position = POSITION_BELOW self._state_position = POSITION_BELOW
if above(self.sensor_value, self._threshold_upper): if above(self.sensor_value, self._threshold_upper):
self._state_position = POSITION_ABOVE self._state_position = POSITION_ABOVE
self._state = True self._attr_is_on = True
elif below(self.sensor_value, self._threshold_upper): elif below(self.sensor_value, self._threshold_upper):
self._state_position = POSITION_BELOW self._state_position = POSITION_BELOW
self._state = False self._attr_is_on = False
return return
if self.threshold_type == TYPE_RANGE: if self.threshold_type == TYPE_RANGE:
if self._state is None: if self._attr_is_on is None:
self._state = True self._attr_is_on = True
self._state_position = POSITION_IN_RANGE self._state_position = POSITION_IN_RANGE
if below(self.sensor_value, self._threshold_lower): if below(self.sensor_value, self._threshold_lower):
self._state_position = POSITION_BELOW self._state_position = POSITION_BELOW
self._state = False self._attr_is_on = False
if above(self.sensor_value, self._threshold_upper): if above(self.sensor_value, self._threshold_upper):
self._state_position = POSITION_ABOVE self._state_position = POSITION_ABOVE
self._state = False self._attr_is_on = False
elif above(self.sensor_value, self._threshold_lower) and below( elif above(self.sensor_value, self._threshold_lower) and below(
self.sensor_value, self._threshold_upper self.sensor_value, self._threshold_upper
): ):
self._state_position = POSITION_IN_RANGE self._state_position = POSITION_IN_RANGE
self._state = True self._attr_is_on = True
return return
@callback @callback