Skip to content

Commit

Permalink
Fixed sync of integration and device name for HA 2024.7.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbkarlsson authored Aug 2, 2024
1 parent e833b4c commit 840176c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
36 changes: 17 additions & 19 deletions custom_components/ev_smart_charging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions custom_components/ev_smart_charging/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
38 changes: 19 additions & 19 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 840176c

Please sign in to comment.