Skip to content

Commit 40cc078

Browse files
authored
Merge pull request #2104 from rosenrot00/patch-24
Fixing an issue that caused entities not to be updated correctly
2 parents aca2ce7 + 3abb84b commit 40cc078

1 file changed

Lines changed: 38 additions & 26 deletions

File tree

custom_components/solax_modbus/sensor.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,34 @@ def empty_input_device_group_lambda() -> SimpleNamespace:
100100
)
101101

102102

103-
def is_entity_enabled(hass: HomeAssistant, hub: Any, descriptor: Any, use_default: bool = False) -> bool:
103+
def is_entity_enabled(
104+
hass: HomeAssistant,
105+
hub: Any,
106+
descriptor: Any,
107+
use_default: bool = False,
108+
platform_name: str | None = None,
109+
) -> bool:
104110
"""Check if entity is enabled in registry."""
105111
# simple test, more complex counterpart is should_register_be_loaded
106-
unique_id = f"{hub._name}_{descriptor.key}"
112+
unique_id_prefix = platform_name or hub._name
113+
unique_id = f"{unique_id_prefix}_{descriptor.key}"
107114
registry = er.async_get(hass)
108115
entity_id = registry.async_get_entity_id("sensor", DOMAIN, unique_id)
109116
if entity_id:
110117
entity_entry = registry.async_get(entity_id)
111-
if entity_entry and not entity_entry.disabled:
112-
_LOGGER.debug(f"{hub.name}: is_entity_enabled: {entity_id} is enabled, returning True.")
113-
return True # Found an enabled entity, no need to check further
114-
else:
115-
_LOGGER.info(f"{hub.name}: entity {unique_id} not found in registry")
118+
if entity_entry is None:
119+
_LOGGER.debug(f"{hub.name}: is_entity_enabled: {entity_id} has no registry entry, returning False.")
120+
return False
121+
if entity_entry.disabled:
122+
_LOGGER.debug(f"{hub.name}: is_entity_enabled: {entity_id} is disabled, returning False.")
123+
return False
124+
_LOGGER.debug(f"{hub.name}: is_entity_enabled: {entity_id} is enabled, returning True.")
125+
return True # Found an enabled entity, no need to check further
126+
127+
_LOGGER.info(f"{hub.name}: entity {unique_id} not found in registry")
116128
if use_default:
117129
_LOGGER.debug(
118-
f"{hub.name}: is_entity_enabled: {entity_id} not found in registry, returning default {descriptor.entity_registry_enabled_default}."
130+
f"{hub.name}: is_entity_enabled: {unique_id} not found in registry, returning default {descriptor.entity_registry_enabled_default}."
119131
)
120132
return bool(descriptor.entity_registry_enabled_default)
121133
return False
@@ -386,12 +398,10 @@ async def readFollowUpBattery(
386398
else:
387399
start_time = time.time()
388400
energy_dashboard_sensors = await create_energy_dashboard_sensors(hub, mapping, hass, config)
401+
energy_dashboard_entities = []
402+
energy_dashboard_platform_name = f"{hub_name} Energy Dashboard"
389403
if energy_dashboard_sensors:
390404
_LOGGER.info(f"{hub_name}: Creating {len(energy_dashboard_sensors)} Energy Dashboard sensors")
391-
# Create a new list to track Energy Dashboard entities
392-
energy_dashboard_entities = []
393-
# Use Energy Dashboard device name as platform name for entity_id prefix
394-
energy_dashboard_platform_name = f"{hub_name} Energy Dashboard"
395405
entityToList(
396406
hub,
397407
energy_dashboard_platform_name,
@@ -406,6 +416,17 @@ async def readFollowUpBattery(
406416
readFollowUp,
407417
)
408418

419+
# Ensure existing Energy Dashboard entities are enabled before adding them.
420+
entity_registry = er.async_get(hass)
421+
for sensor_description in energy_dashboard_sensors:
422+
unique_id = f"{energy_dashboard_platform_name}_{sensor_description.key}"
423+
entity_id = entity_registry.async_get_entity_id("sensor", DOMAIN, unique_id)
424+
if entity_id:
425+
maybe_entry = entity_registry.async_get(entity_id)
426+
if maybe_entry is not None and maybe_entry.disabled_by:
427+
_LOGGER.debug(f"{hub_name}: Enabling previously disabled Energy Dashboard entity: {entity_id}")
428+
entity_registry.async_update_entity(entity_id, disabled_by=None)
429+
409430
# Add Energy Dashboard entities to main entities list and register them
410431
if energy_dashboard_entities:
411432
_LOGGER.info(f"{hub_name}: Registering {len(energy_dashboard_entities)} Energy Dashboard entities")
@@ -417,18 +438,6 @@ async def readFollowUpBattery(
417438
f"{hub_name}: Energy Dashboard device creation completed in {elapsed_time:.3f}s ({len(energy_dashboard_entities)} entities)"
418439
)
419440

420-
# Ensure Energy Dashboard entities are enabled (they might have been disabled previously)
421-
entity_registry = er.async_get(hass)
422-
hub_unique_prefix = f"{hub._name}_"
423-
for sensor_mapping in mapping.mappings:
424-
unique_id = f"{hub_unique_prefix}{sensor_mapping.target_key}"
425-
entity_id = entity_registry.async_get_entity_id("sensor", DOMAIN, unique_id)
426-
if entity_id:
427-
maybe_entry = entity_registry.async_get(entity_id)
428-
if maybe_entry is not None and maybe_entry.disabled_by:
429-
_LOGGER.debug(f"{hub_name}: Enabling previously disabled Energy Dashboard entity: {entity_id}")
430-
entity_registry.async_update_entity(entity_id, disabled_by=None)
431-
432441
async def async_refresh_energy_dashboard_entities() -> None:
433442
energy_dashboard_enabled = config.get(CONF_ENERGY_DASHBOARD_DEVICE, DEFAULT_ENERGY_DASHBOARD_DEVICE)
434443
if isinstance(energy_dashboard_enabled, str):
@@ -455,7 +464,7 @@ async def async_refresh_energy_dashboard_entities() -> None:
455464
energy_dashboard_platform_name = f"{hub_name} Energy Dashboard"
456465
for newdescr in energy_dashboard_sensors:
457466
existing_sensor = hub.sensorEntities.get(newdescr.key)
458-
if existing_sensor:
467+
if existing_sensor and getattr(existing_sensor, "hass", None) is not None:
459468
existing_sensor.entity_description = newdescr
460469
if hasattr(existing_sensor, "_riemann_mapping") and getattr(newdescr, "_riemann_mapping", None):
461470
existing_sensor._riemann_mapping = newdescr._riemann_mapping
@@ -465,6 +474,9 @@ async def async_refresh_energy_dashboard_entities() -> None:
465474
if newdescr.register < 0 and newdescr.value_function:
466475
hub.computedSensors[newdescr.key] = newdescr
467476
continue
477+
if existing_sensor:
478+
hub.sensorEntities.pop(newdescr.key, None)
479+
hub.computedSensors.pop(newdescr.key, None)
468480

469481
entityToListSingle(
470482
hub,
@@ -886,7 +898,7 @@ def entityToListSingle(
886898
if newdescr.sleepmode == SLEEPMODE_ZERO:
887899
hub.sleepzero.append(newdescr.key)
888900
if newdescr.register < 0: # entity without modbus address
889-
enabled = is_entity_enabled(hub._hass, hub, newdescr, use_default=True) # dont compute disabled entities anymore
901+
enabled = is_entity_enabled(hub._hass, hub, newdescr, use_default=True, platform_name=hub_name) # dont compute disabled entities anymore
890902
# if not enabled: _LOGGER.info(f"is_entity_enabled called for disabled entity {newdescr.key}")
891903
if newdescr.value_function and (enabled or newdescr.internal): # *** dont compute disabled entities anymore unless internal
892904
computedRegs[newdescr.key] = newdescr

0 commit comments

Comments
 (0)