From 5986646af450d8f97a20ec03a237d3a121706219 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Fri, 18 Oct 2024 09:21:07 +0200 Subject: [PATCH] Use shorthand attribute in trend binary sensor (#128614) Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> Co-authored-by: Franck Nijhof --- homeassistant/components/trend/binary_sensor.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/trend/binary_sensor.py b/homeassistant/components/trend/binary_sensor.py index 693c080e86e..681680f180f 100644 --- a/homeassistant/components/trend/binary_sensor.py +++ b/homeassistant/components/trend/binary_sensor.py @@ -199,11 +199,6 @@ class SensorTrend(BinarySensorEntity, RestoreEntity): if sensor_entity_id: self.entity_id = sensor_entity_id - @property - def is_on(self) -> bool | None: - """Return true if sensor is on.""" - return self._state - @property def extra_state_attributes(self) -> Mapping[str, Any]: """Return the state attributes of the sensor.""" @@ -247,9 +242,9 @@ class SensorTrend(BinarySensorEntity, RestoreEntity): if not (state := await self.async_get_last_state()): return - if state.state == STATE_UNKNOWN: + if state.state in {STATE_UNKNOWN, STATE_UNAVAILABLE}: return - self._state = state.state == STATE_ON + self._attr_is_on = state.state == STATE_ON async def async_update(self) -> None: """Get the latest data and update the states.""" @@ -266,13 +261,13 @@ class SensorTrend(BinarySensorEntity, RestoreEntity): await self.hass.async_add_executor_job(self._calculate_gradient) # Update state - self._state = ( + self._attr_is_on = ( abs(self._gradient) > abs(self._min_gradient) and math.copysign(self._gradient, self._min_gradient) == self._gradient ) if self._invert: - self._state = not self._state + self._attr_is_on = not self._attr_is_on def _calculate_gradient(self) -> None: """Compute the linear trend gradient of the current samples.