Skip to content

Commit

Permalink
fix: Platform solarman does not generate unique IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 5, 2024
1 parent 67f0953 commit 02a7d8a
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions custom_components/solarman/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ def migrate_unique_ids(name: str, serial: int, entity_entry: RegistryEntry) -> d

entity_name = entity_entry.original_name if entity_entry.has_entity_name or not entity_entry.original_name else entity_entry.original_name.replace(name, '').strip()
old_unique_id = '_'.join(filter(None, (name, str(serial), entity_name)))
slugified_old_unique_id = slugify(old_unique_id)

if entity_entry.unique_id == old_unique_id and (new_unique_id := slugify(old_unique_id if entity_name else f"{old_unique_id}_{split_entity_id(entity_entry.entity_id)[0]}")):
_LOGGER.debug("Migrating unique_id for %s entity from [%s] to [%s]", entity_entry.entity_id, old_unique_id, new_unique_id)
return { "new_unique_id": entity_entry.unique_id.replace(old_unique_id, new_unique_id) }
for old_unique_id in (old_unique_id, slugified_old_unique_id):
if entity_entry.unique_id == old_unique_id and (new_unique_id := f"{slugified_old_unique_id}_{split_entity_id(entity_entry.entity_id)[0]}"):
_LOGGER.debug("Migrating unique_id for %s entity from [%s] to [%s]", entity_entry.entity_id, old_unique_id, new_unique_id)
return { "new_unique_id": entity_entry.unique_id.replace(old_unique_id, new_unique_id) }

return None

Expand Down Expand Up @@ -87,7 +89,7 @@ def __init__(self, coordinator, sensor, platform):
self._attr_has_entity_name = True
self._attr_device_class = sensor.get("class") or sensor.get("device_class")
self._attr_translation_key = sensor.get("translation_key") or slugify(self._attr_name)
self._attr_unique_id = slugify('_'.join(filter(None, (self.device_name, str(self.coordinator.inverter.serial), self._attr_name or platform))))
self._attr_unique_id = slugify('_'.join(filter(None, (self.device_name, str(self.coordinator.inverter.serial), self._attr_name, platform))))
self._attr_entity_category = sensor.get("category") or sensor.get("entity_category")
self._attr_entity_registry_enabled_default = not "disabled" in sensor
self._attr_entity_registry_visible_default = not "hidden" in sensor
Expand Down

0 comments on commit 02a7d8a

Please sign in to comment.