Skip to content

Commit

Permalink
Merge branch 'cdnninja-patch-1' of https://github.com/audiconnect/aud…
Browse files Browse the repository at this point in the history
…i_connect_ha into cdnninja-patch-1
  • Loading branch information
cdnninja committed Apr 10, 2024
1 parent d391cb3 commit 7ad05d0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
47 changes: 47 additions & 0 deletions custom_components/audiconnect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,50 @@ async def async_remove_config_entry_device(
) -> bool:
"""Remove a config entry from a device."""
return True


async def async_migrate_entry(
self, hass: HomeAssistant, config_entry: ConfigEntry
) -> bool:
"""Migrates an old configuration entry to version 2."""

if config_entry.version == 1:
# Update config entry data with spread operator
new_data = {**config_entry.data}
hass.config_entries.async_update_entry(
config_entry, data=new_data, minor_version=0, version=2
)
_LOGGER.info(
f"Migration to version {config_entry.version}.{config_entry.minor_version} successful"
)

# Get device registry and iterate through devices
device_registry = await dr.async_get(hass)
for entry_id, device in device_registry.devices.items():
if device.domain == DOMAIN and "identifiers" in device.config_entries:
old_identifier = device.config_entries["identifiers"][0]

# Check for old identifier using f-string for clarity
if old_identifier[1] == self._instrument.vehicle_name:
_LOGGER.info(
f"Migrating device {device.name} ({device.id}) to new identifier"
)
new_identifier = (DOMAIN, self._instrument.vehicle_vin)
try:
await device_registry.async_update_device(
entry_id, device_id=new_identifier["id"]
)
_LOGGER.info(f"Migration for device {device.name} successful")
except Exception as e:
_LOGGER.error(f"Migration for device {device.name} failed: {e}")
else:
_LOGGER.info(
f"No migration necessary for device {device.name} ({device.id}) to new identifier"
)

else:
_LOGGER.info(
f"No migration necessary for config entry version {config_entry.version}"
)

return True
11 changes: 6 additions & 5 deletions custom_components/audiconnect/audi_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, data, instrument):
"""Initialize the entity."""
self._data = data
self._instrument = instrument
self._vin = self._instrument.vehicle_name
self._vin = self._instrument.vehicle_vin
self._component = self._instrument.component
self._attribute = self._instrument.attr

Expand Down Expand Up @@ -53,6 +53,11 @@ def assumed_state(self):
"""Return true if unable to access real state of entity."""
return True

@property
def unique_id(self):
return self._instrument.vehicle_vin


@property
def extra_state_attributes(self):
"""Return device specific state attributes."""
Expand All @@ -68,10 +73,6 @@ def extra_state_attributes(self):
vin=self._instrument.vehicle_vin,
)

@property
def unique_id(self):
return self._instrument.full_name

@property
def device_info(self):
model_only = self._instrument.vehicle_model.replace("Audi ", "")
Expand Down

0 comments on commit 7ad05d0

Please sign in to comment.