diff --git a/homeassistant/components/sofar/__init__.py b/homeassistant/components/sofar/__init__.py index fa90b883ede7..fdff1b866c93 100644 --- a/homeassistant/components/sofar/__init__.py +++ b/homeassistant/components/sofar/__init__.py @@ -101,10 +101,9 @@ def _async_store_served( hass: HomeAssistant, entry: SofarConfigEntry, served: frozenset[str] ) -> None: """Remember what the inverter serves for a setup it sleeps through.""" - if (stored := sorted(served)) != entry.data.get(SERVED_COMPONENTS): - hass.config_entries.async_update_entry( - entry, data={**entry.data, SERVED_COMPONENTS: stored} - ) + hass.config_entries.async_update_entry( + entry, data={**entry.data, SERVED_COMPONENTS: sorted(served)} + ) async def _async_refresh_versions(hass: HomeAssistant, entry: SofarConfigEntry) -> None: diff --git a/homeassistant/components/sofar/sensor.py b/homeassistant/components/sofar/sensor.py index 9089c79f0fe3..18de476c2668 100644 --- a/homeassistant/components/sofar/sensor.py +++ b/homeassistant/components/sofar/sensor.py @@ -79,7 +79,7 @@ async def async_setup_entry( entities: list[SofarSensor | SofarTotalSensor] = [ SofarSensor(runtime_data, description) for description in SENSOR_DESCRIPTIONS - if description.component in served and not _is_battery_string(description) + if description.component in served and _battery_string(description) is None ] entities.extend( SofarTotalSensor(runtime_data, description) @@ -101,9 +101,7 @@ async def async_setup_entry( async_add_entities( SofarSensor(runtime_data, description) for description in SENSOR_DESCRIPTIONS - if (part := description.part) is not None - and part[0] == "battery" - and part[1] in new + if _battery_string(description) in new ) _async_add_wired_battery_strings() @@ -146,9 +144,11 @@ def _async_setup_battery_packs( ) -def _is_battery_string(description: SofarSensorDescription) -> bool: - """Whether a description belongs to one numbered battery string.""" - return description.part is not None and description.part[0] == "battery" +def _battery_string(description: SofarSensorDescription) -> int | None: + """Return the battery string a description belongs to, if any.""" + if (part := description.part) is None or part[0] != "battery": + return None + return part[1] class SofarSensor(SofarEntity, SensorEntity): diff --git a/homeassistant/components/sofar/switch.py b/homeassistant/components/sofar/switch.py index 82c97319682d..6dfd21606c82 100644 --- a/homeassistant/components/sofar/switch.py +++ b/homeassistant/components/sofar/switch.py @@ -65,7 +65,7 @@ class SofarSwitch(SofarEntity, SwitchEntity): def is_on(self) -> bool | None: """Return whether the remote switch is on.""" value = self.entity_description.value_fn(self.coordinator.device) - return None if value is None else bool(value) + return None if value is None else value is RemoteSwitchOnOff.ON async def _async_write(self, value: bool) -> None: """Write the switch state to the device."""