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:
J. Nick Koston 2020-10-24 16:35:21 -05:00 committed by GitHub
parent 5e9de88f4b
commit c1a7896a40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View File

@ -133,6 +133,9 @@ class SenseDevice(BinarySensorEntity):
@callback
def _async_update_from_data(self):
"""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._state = bool(self._sense_devices_data.get_device_by_id(self._id))
self._state = new_state
self.async_write_ha_state()

View File

@ -195,11 +195,14 @@ class SenseActiveSensor(Entity):
@callback
def _async_update_from_data(self):
"""Update the sensor from the data. Must not do I/O."""
self._state = round(
new_state = round(
self._data.active_solar_power
if self._is_production
else self._data.active_power
)
if self._available and self._state == new_state:
return
self._state = new_state
self._available = True
self.async_write_ha_state()
@ -276,8 +279,11 @@ class SenseVoltageSensor(Entity):
@callback
def _async_update_from_data(self):
"""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._state = new_state
self.async_write_ha_state()
@ -438,8 +444,11 @@ class SenseEnergyDevice(Entity):
"""Get the latest data, update state. Must not do I/O."""
device_data = self._sense_devices_data.get_device_by_id(self._id)
if not device_data or "w" not in device_data:
self._state = 0
new_state = 0
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.async_write_ha_state()