Skip to content

Commit

Permalink
Improve performance of Solis Modbus sensor
Browse files Browse the repository at this point in the history
Removed unnecessary polling counter and scan interval in the Solis Modbus sensor. Also rearranged the order of operations in the time setting process to further optimize the code. The async write operation for the home automation state is now executed immediately after setting the time value. fixes #28
  • Loading branch information
Pho3niX90 committed Jan 3, 2024
1 parent 58d24ce commit d3a5a35
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
7 changes: 0 additions & 7 deletions custom_components/solis_modbus/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
sensor_entities: List[SensorEntity] = []
sensor_derived_entities: List[SensorEntity] = []
hass.data[DOMAIN]['values'] = {}
hass.data[DOMAIN]['POLL_COUNTER'] = 0

sensors = [
{
Expand Down Expand Up @@ -579,7 +578,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
},
]


sensors_derived = [
{"type": "SDS", "name": "Solis Status String",
"unique": "solis_modbus_inverter_current_status_string", "multiplier": 0,
Expand Down Expand Up @@ -635,7 +633,6 @@ def async_update(now):

for sensor_group in sensors:
start_register = sensor_group['register_start']
scan_interval = sensor_group['scan_interval']

count = sum(len(entity.get('register', [])) for entity in sensor_group.get('entities', []))

Expand All @@ -655,11 +652,7 @@ def async_update(now):
for entity in hass.data[DOMAIN]["sensor_derived_entities"]:
entity.update()

hass.data[DOMAIN]['POLL_COUNTER'] += 1

# Schedule the update function to run every X seconds
async_track_time_interval(hass, async_update, timedelta(seconds=POLL_INTERVAL_SECONDS))

return True


Expand Down
5 changes: 3 additions & 2 deletions custom_components/solis_modbus/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def update(self):
_LOGGER.debug(f'Update time entity with hour = {hour}, minute = {minute}')

self._attr_native_value = time(hour=hour, minute=minute)
# self.async_write_ha_state()

@property
def device_info(self):
Expand All @@ -136,10 +137,10 @@ def set_native_value(self, value):
if self._attr_native_value == value:
return
_LOGGER.debug(f'set_native_value : register = {self._register}, value = {value}')
self._attr_native_value = value
self.schedule_update_ha_state()

async def async_set_value(self, value: time) -> None:
"""Set the time."""
_LOGGER.debug(f'async_set_value : register = {self._register}, value = {value}')
self._modbus_controller.write_holding_registers(self._register, [value.hour, value.minute])
self._attr_native_value = value
self.async_write_ha_state()

0 comments on commit d3a5a35

Please sign in to comment.