mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Minimize sense writes to the state machine (#42310)
Sense was generating hundred state updates when most of the time the state was not changing
This commit is contained in:
parent
5e9de88f4b
commit
c1a7896a40
@ -133,6 +133,9 @@ class SenseDevice(BinarySensorEntity):
|
|||||||
@callback
|
@callback
|
||||||
def _async_update_from_data(self):
|
def _async_update_from_data(self):
|
||||||
"""Get the latest data, update state. Must not do I/O."""
|
"""Get the latest data, update state. Must not do I/O."""
|
||||||
|
new_state = bool(self._sense_devices_data.get_device_by_id(self._id))
|
||||||
|
if self._available and self._state == new_state:
|
||||||
|
return
|
||||||
self._available = True
|
self._available = True
|
||||||
self._state = bool(self._sense_devices_data.get_device_by_id(self._id))
|
self._state = new_state
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
@ -195,11 +195,14 @@ class SenseActiveSensor(Entity):
|
|||||||
@callback
|
@callback
|
||||||
def _async_update_from_data(self):
|
def _async_update_from_data(self):
|
||||||
"""Update the sensor from the data. Must not do I/O."""
|
"""Update the sensor from the data. Must not do I/O."""
|
||||||
self._state = round(
|
new_state = round(
|
||||||
self._data.active_solar_power
|
self._data.active_solar_power
|
||||||
if self._is_production
|
if self._is_production
|
||||||
else self._data.active_power
|
else self._data.active_power
|
||||||
)
|
)
|
||||||
|
if self._available and self._state == new_state:
|
||||||
|
return
|
||||||
|
self._state = new_state
|
||||||
self._available = True
|
self._available = True
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@ -276,8 +279,11 @@ class SenseVoltageSensor(Entity):
|
|||||||
@callback
|
@callback
|
||||||
def _async_update_from_data(self):
|
def _async_update_from_data(self):
|
||||||
"""Update the sensor from the data. Must not do I/O."""
|
"""Update the sensor from the data. Must not do I/O."""
|
||||||
self._state = round(self._data.active_voltage[self._voltage_index], 1)
|
new_state = round(self._data.active_voltage[self._voltage_index], 1)
|
||||||
|
if self._available and self._state == new_state:
|
||||||
|
return
|
||||||
self._available = True
|
self._available = True
|
||||||
|
self._state = new_state
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
|
||||||
@ -438,8 +444,11 @@ class SenseEnergyDevice(Entity):
|
|||||||
"""Get the latest data, update state. Must not do I/O."""
|
"""Get the latest data, update state. Must not do I/O."""
|
||||||
device_data = self._sense_devices_data.get_device_by_id(self._id)
|
device_data = self._sense_devices_data.get_device_by_id(self._id)
|
||||||
if not device_data or "w" not in device_data:
|
if not device_data or "w" not in device_data:
|
||||||
self._state = 0
|
new_state = 0
|
||||||
else:
|
else:
|
||||||
self._state = int(device_data["w"])
|
new_state = int(device_data["w"])
|
||||||
|
if self._available and self._state == new_state:
|
||||||
|
return
|
||||||
|
self._state = new_state
|
||||||
self._available = True
|
self._available = True
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user