Skip to content

Commit

Permalink
Ensure unique_id is a string (#251)
Browse files Browse the repository at this point in the history
Migrate old config entries as needed
  • Loading branch information
mill1000 authored Oct 7, 2024
1 parent 07b512e commit d1fc0d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
22 changes: 22 additions & 0 deletions custom_components/midea_ac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
return True


async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Migrate config entry."""

_LOGGER.debug("Migrating configuration from version %s.%s.",
config_entry.version, config_entry.minor_version)

if config_entry.version > 1:
# Unsupported downgrade
return False

if config_entry.version == 1:
# 1.1 -> 1.2: Convert unique ID to string
if config_entry.minor_version == 1:
hass.config_entries.async_update_entry(
config_entry, unique_id=str(config_entry.unique_id), minor_version=2)

_LOGGER.debug("Migration to configuration version %s.%s successful.",
config_entry.version, config_entry.minor_version)

return True


async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Unload a config entry."""
# Remove the coordinator from global data
Expand Down
11 changes: 7 additions & 4 deletions custom_components/midea_ac/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
class MideaConfigFlow(ConfigFlow, domain=DOMAIN):
"""Config flow for Midea Smart AC."""

VERSION = 1
MINOR_VERSION = 2

async def async_step_user(self, _) -> FlowResult:
"""Handle a config flow initialized by the user."""
return self.async_show_menu(
Expand Down Expand Up @@ -62,7 +65,7 @@ async def async_step_discover(
errors["base"] = "unsupported_device"
else:
# Check if device has already been configured
await self.async_set_unique_id(device.id)
await self.async_set_unique_id(str(device.id))
self._abort_if_unique_id_configured()

# Finish connection
Expand Down Expand Up @@ -92,7 +95,7 @@ async def async_step_pick_device(

if device:
# Check if device has already been configured
await self.async_set_unique_id(device.id)
await self.async_set_unique_id(str(device.id))
self._abort_if_unique_id_configured()

# Finish connection
Expand All @@ -116,7 +119,7 @@ async def async_step_pick_device(
f"{device.name} - {device.id} ({device.ip})"
)
for device in self._discovered_devices
if (device.id not in configured_devices and
if (str(device.id) not in configured_devices and
device.type == DeviceType.AIR_CONDITIONER)
}

Expand All @@ -140,7 +143,7 @@ async def async_step_manual(self, user_input) -> FlowResult:
id = int(user_input.get(CONF_ID))

# Check if device has already been configured
await self.async_set_unique_id(id)
await self.async_set_unique_id(str(id))
self._abort_if_unique_id_configured()

# Attempt a connection to see if config is valid
Expand Down

0 comments on commit d1fc0d9

Please sign in to comment.