From 840176ca7eef97921165431c063e28c4de933e7d Mon Sep 17 00:00:00 2001 From: Jonas Karlsson Date: Fri, 2 Aug 2024 10:56:11 +0200 Subject: [PATCH] Fixed sync of integration and device name for HA 2024.7. --- .../ev_smart_charging/__init__.py | 36 +++++++++--------- .../ev_smart_charging/coordinator.py | 8 ++-- tests/test_init.py | 38 +++++++++---------- 3 files changed, 39 insertions(+), 43 deletions(-) diff --git a/custom_components/ev_smart_charging/__init__.py b/custom_components/ev_smart_charging/__init__.py index df836ad..c9cf736 100644 --- a/custom_components/ev_smart_charging/__init__.py +++ b/custom_components/ev_smart_charging/__init__.py @@ -62,25 +62,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): # If the name of the integration (config_entry.title) has changed, # update the device name. - if MAJOR_VERSION < 2024 or (MAJOR_VERSION == 2024 and MINOR_VERSION <= 6): - # Not needed for HA 2024.7 - entity_registry: EntityRegistry = async_entity_registry_get(hass) - all_entities = async_entries_for_config_entry(entity_registry, entry.entry_id) - if all_entities: - device_id = all_entities[0].device_id - device_registry: DeviceRegistry = async_device_registry_get(hass) - device: DeviceEntry = device_registry.async_get(device_id) - if device: - if device.name_by_user is not None: - if entry.title != device.name_by_user: - device_registry.async_update_device( - device.id, name_by_user=entry.title - ) - else: - if entry.title != device.name: - device_registry.async_update_device( - device.id, name_by_user=entry.title - ) + entity_registry: EntityRegistry = async_entity_registry_get(hass) + all_entities = async_entries_for_config_entry(entity_registry, entry.entry_id) + if all_entities: + device_id = all_entities[0].device_id + device_registry: DeviceRegistry = async_device_registry_get(hass) + device: DeviceEntry = device_registry.async_get(device_id) + if device: + if device.name_by_user is not None: + if entry.title != device.name_by_user: + device_registry.async_update_device( + device.id, name_by_user=entry.title + ) + else: + if entry.title != device.name: + device_registry.async_update_device( + device.id, name_by_user=entry.title + ) return True diff --git a/custom_components/ev_smart_charging/coordinator.py b/custom_components/ev_smart_charging/coordinator.py index 3415a98..ad5fa4e 100644 --- a/custom_components/ev_smart_charging/coordinator.py +++ b/custom_components/ev_smart_charging/coordinator.py @@ -187,11 +187,9 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None: async_track_time_change(hass, self.update_hourly, minute=0, second=0) ) # Listen for changes to the device. - if MAJOR_VERSION < 2024 or (MAJOR_VERSION == 2024 and MINOR_VERSION <= 6): - # Is not needed for HA 2024.7 - self.listeners.append( - hass.bus.async_listen(EVENT_DEVICE_REGISTRY_UPDATED, self.device_updated) - ) + self.listeners.append( + hass.bus.async_listen(EVENT_DEVICE_REGISTRY_UPDATED, self.device_updated) + ) def unsubscribe_listeners(self): """Unsubscribed to listeners""" diff --git a/tests/test_init.py b/tests/test_init.py index 4274444..f5817c2 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -232,28 +232,28 @@ async def test_setup_new_integration_name(hass, bypass_validate_input_sensors): hass.data[DOMAIN][config_entry.entry_id], EVSmartChargingCoordinator ) - # Only test for HA 2024.6 and older + test = hass.data["device_registry"].devices + device = hass.data["device_registry"].devices[next(iter(test))] + # The behvior of HA 2024.6 and older (name_by_user is updated) + # and HA 2024.7 and newer (name is updated) is different. + assert device.name_by_user == "New title" or device.name == "New title" - if MAJOR_VERSION < 2024 or (MAJOR_VERSION == 2024 and MINOR_VERSION <= 6): + # Change a changed title + config_entry.title = "New title2" - test = hass.data["device_registry"].devices - device = hass.data["device_registry"].devices[next(iter(test))] - assert device.name_by_user == "New title" - - # Change a changed title - config_entry.title = "New title2" - - # Reload the entry and assert that the data from above is still there - assert await async_reload_entry(hass, config_entry) is None - await hass.async_block_till_done() - assert DOMAIN in hass.data and config_entry.entry_id in hass.data[DOMAIN] - assert isinstance( - hass.data[DOMAIN][config_entry.entry_id], EVSmartChargingCoordinator - ) + # Reload the entry and assert that the data from above is still there + assert await async_reload_entry(hass, config_entry) is None + await hass.async_block_till_done() + assert DOMAIN in hass.data and config_entry.entry_id in hass.data[DOMAIN] + assert isinstance( + hass.data[DOMAIN][config_entry.entry_id], EVSmartChargingCoordinator + ) - test = hass.data["device_registry"].devices - device = hass.data["device_registry"].devices[next(iter(test))] - assert device.name_by_user == "New title2" + test = hass.data["device_registry"].devices + device = hass.data["device_registry"].devices[next(iter(test))] + # The behvior of HA 2024.6 and older (name_by_user is updated) + # and HA 2024.7 and newer (name is updated) is different. + assert device.name_by_user == "New title2" or device.name == "New title2" # Unload the entry and verify that the data has been removed assert await async_unload_entry(hass, config_entry)