Make the Sofar checks explicit (#191856)

This commit is contained in:
darkrain-nl
2026-11-10 12:20:06 +00:00
committed by GitHub
parent 48e7362dde
commit a79c72fcfb
3 changed files with 11 additions and 12 deletions
+3 -4
View File
@@ -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:
+7 -7
View File
@@ -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):
+1 -1
View File
@@ -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."""