mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 23:57:06 +00:00
Use shorthand attribute in trend binary sensor (#128614)
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
5674c1d82f
commit
5986646af4
@ -199,11 +199,6 @@ class SensorTrend(BinarySensorEntity, RestoreEntity):
|
|||||||
if sensor_entity_id:
|
if sensor_entity_id:
|
||||||
self.entity_id = 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
|
@property
|
||||||
def extra_state_attributes(self) -> Mapping[str, Any]:
|
def extra_state_attributes(self) -> Mapping[str, Any]:
|
||||||
"""Return the state attributes of the sensor."""
|
"""Return the state attributes of the sensor."""
|
||||||
@ -247,9 +242,9 @@ class SensorTrend(BinarySensorEntity, RestoreEntity):
|
|||||||
|
|
||||||
if not (state := await self.async_get_last_state()):
|
if not (state := await self.async_get_last_state()):
|
||||||
return
|
return
|
||||||
if state.state == STATE_UNKNOWN:
|
if state.state in {STATE_UNKNOWN, STATE_UNAVAILABLE}:
|
||||||
return
|
return
|
||||||
self._state = state.state == STATE_ON
|
self._attr_is_on = state.state == STATE_ON
|
||||||
|
|
||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Get the latest data and update the states."""
|
"""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)
|
await self.hass.async_add_executor_job(self._calculate_gradient)
|
||||||
|
|
||||||
# Update state
|
# Update state
|
||||||
self._state = (
|
self._attr_is_on = (
|
||||||
abs(self._gradient) > abs(self._min_gradient)
|
abs(self._gradient) > abs(self._min_gradient)
|
||||||
and math.copysign(self._gradient, self._min_gradient) == self._gradient
|
and math.copysign(self._gradient, self._min_gradient) == self._gradient
|
||||||
)
|
)
|
||||||
|
|
||||||
if self._invert:
|
if self._invert:
|
||||||
self._state = not self._state
|
self._attr_is_on = not self._attr_is_on
|
||||||
|
|
||||||
def _calculate_gradient(self) -> None:
|
def _calculate_gradient(self) -> None:
|
||||||
"""Compute the linear trend gradient of the current samples.
|
"""Compute the linear trend gradient of the current samples.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user