Skip to content

Commit b187d6f

Browse files
authored
Merge pull request #2227 from Bl00d-B0b/fix/switch-local-data-pollution
fix: switches no longer pollute local persistent data; restore last state at startup
2 parents 7fdc441 + 1f6c63e commit b187d6f

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

custom_components/solax_modbus/switch.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
4343
switch = SolaXModbusSwitch(hub_name, hub, modbus_addr, hub.device_info, switch_info)
4444
if switch_info.value_function:
4545
hub.computedSwitches[switch_info.key] = switch_info
46-
if switch_info.sensor_key is not None:
46+
if switch_info.write_method == WRITE_DATA_LOCAL and switch_info.sensor_key is not None:
4747
hub.writeLocals[switch_info.sensor_key] = switch_info
4848
dependency_key = getattr(switch_info, "sensor_key", switch_info.key)
4949
if dependency_key != switch_info.key:
@@ -87,7 +87,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
8787
)
8888
if switch_info.value_function:
8989
hub.computedSwitches[switch_info.key] = switch_info
90-
if switch_info.sensor_key is not None:
90+
if switch_info.write_method == WRITE_DATA_LOCAL and switch_info.sensor_key is not None:
9191
hub.writeLocals[switch_info.sensor_key] = switch_info
9292
dependency_key = getattr(switch_info, "sensor_key", switch_info.key)
9393
if dependency_key != switch_info.key:
@@ -148,6 +148,11 @@ async def _async_set_state(self, is_on: bool) -> None:
148148
async def async_added_to_hass(self) -> None:
149149
await super().async_added_to_hass()
150150
if self.entity_description.write_method != WRITE_DATA_LOCAL:
151+
if self._sensor_key is not None and self._sensor_key in self._hub.data:
152+
return
153+
last_state = await self.async_get_last_state()
154+
if last_state and last_state.state in ("on", "off"):
155+
self._attr_is_on = last_state.state == "on"
151156
return
152157
self.async_on_remove(self.hass.bus.async_listen("solax_modbus_local_data_loaded", self._handle_local_data_loaded))
153158
if self._sensor_key is not None and self._sensor_key in self._hub.data:

0 commit comments

Comments
 (0)