Skip to content

Commit

Permalink
Use shorthand attribute in trend binary sensor (home-assistant#128614)
Browse files Browse the repository at this point in the history
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
Co-authored-by: Franck Nijhof <git@frenck.dev>
  • Loading branch information
3 people authored Oct 18, 2024
1 parent 5674c1d commit 5986646
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions homeassistant/components/trend/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ def __init__(
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."""
Expand Down Expand Up @@ -247,9 +242,9 @@ def trend_sensor_state_listener(

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."""
Expand All @@ -266,13 +261,13 @@ async def async_update(self) -> None:
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.
Expand Down

0 comments on commit 5986646

Please sign in to comment.