fix(homeassistant): apply 0 and off from config entities, never add settings

The configuration sync of the Home Assistant adapter skipped every value that is false in
Python, so an entity state of 0 or off never reached the configuration. It also set any
path it was given: the documented example devices/batteries/0/capacity_wh named a device
"0" that does not exist, so EOS silently added a new battery with a random device id.

Apply every value except an unavailable or unknown entity, and only update settings that
exist; report other paths as an error. The example now addresses the battery by its
device_id.

Signed-off-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com>

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SchaafAlexander
2026-11-11 09:38:45 +01:00
committed by Bobby Noelte
co-authored by Claude Opus 5.5
parent 1fda5f9201
commit 62cbf4cf2a
5 changed files with 126 additions and 12 deletions
+18 -4
View File
@@ -39,11 +39,12 @@ class HomeAssistantAdapterCommonSettings(SettingsBaseModel):
"description": (
"Mapping of EOS config keys to Home Assistant entity IDs.\n"
"The config key has to be given by a ‘/’-separated path\n"
"e.g. devices/batteries/0/capacity_wh"
"e.g. devices/batteries/battery1/capacity_wh\n"
"Devices are addressed by their device_id. The setting must exist."
),
"examples": [
{
"devices/batteries/0/capacity_wh": "sensor.battery1_capacity",
"devices/batteries/battery1/capacity_wh": "sensor.battery1_capacity",
}
],
},
@@ -484,8 +485,21 @@ class HomeAssistantAdapter(AdapterProvider):
state = self.get_entity_state(entity_id)
logger.debug(f"Entity {entity_id}: {state}")
value = self._convert_entity_state(state)
if value:
self.config.set_nested_value(config_key, value)
if value is None:
# Entity unavailable or unknown: keep the configured value.
# 0 and False are values and are applied.
continue
try:
# Only update an existing setting. Setting a path that names
# no setting, e.g. a device that is not configured, would
# silently add a new device.
self.config.get_nested_value(config_key)
except (AttributeError, IndexError, KeyError) as e:
raise ValueError(
f"Config key '{config_key}' of entity '{entity_id}' is not "
f"an existing setting: {e}"
) from e
self.config.set_nested_value(config_key, value)
except Exception as e:
logger.error(f"{e}")