Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions homeassistant/components/daikin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
CONF_UUID,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
Expand Down Expand Up @@ -52,6 +53,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if not daikin_api:
return False

await async_migrate_unique_id(hass, entry, daikin_api)

hass.data.setdefault(DOMAIN, {}).update({entry.entry_id: daikin_api})
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
Expand All @@ -67,7 +70,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return unload_ok


async def daikin_api_setup(hass, host, key, uuid, password):
async def daikin_api_setup(hass: HomeAssistant, host, key, uuid, password):
"""Create a Daikin instance only once."""

session = async_get_clientsession(hass)
Expand Down Expand Up @@ -127,3 +130,63 @@ def device_info(self) -> DeviceInfo:
name=info.get("name"),
sw_version=info.get("ver", "").replace("_", "."),
)


async def async_migrate_unique_id(
hass: HomeAssistant, config_entry: ConfigEntry, api: DaikinApi
) -> None:
"""Migrate old entry."""
dev_reg = dr.async_get(hass)
old_unique_id = config_entry.unique_id
new_unique_id = api.device.mac
new_name = api.device.values.get("name")
Comment thread
mover85 marked this conversation as resolved.
Outdated

@callback
def update_unique_id(entity_entry: er.RegistryEntry) -> dict[str, str] | None:
"""Update unique ID of entity entry."""
if entity_entry.unique_id.startswith(new_unique_id):
# Already correct, nothing to do
return None

unique_id_parts = entity_entry.unique_id.split("-")
unique_id_parts[0] = new_unique_id
entity_new_unique_id = "-".join(unique_id_parts)

_LOGGER.info(
"Migrating entity %s from %s to new id %s",
entity_entry.entity_id,
entity_entry.unique_id,
entity_new_unique_id,
)
return {"new_unique_id": entity_new_unique_id}

if new_unique_id != old_unique_id:
Comment thread
mover85 marked this conversation as resolved.
Outdated
# Migrate devices
for device_entry in dr.async_entries_for_config_entry(
dev_reg, config_entry.entry_id
):
for connection in device_entry.connections:
if connection[1] == old_unique_id:
new_connections = {
(CONNECTION_NETWORK_MAC, dr.format_mac(new_unique_id))
}
_LOGGER.info(
Comment thread
mover85 marked this conversation as resolved.
Outdated
"Migrating device %s to %s and connections to %s",
device_entry.name,
new_name,
new_connections,
)
dev_reg.async_update_device(
device_entry.id,
name=new_name,
merge_connections=new_connections,
)

# Migrate entities
await er.async_migrate_entries(hass, config_entry.entry_id, update_unique_id)

new_data = {**config_entry.data, KEY_MAC: dr.format_mac(new_unique_id)}

hass.config_entries.async_update_entry(
config_entry, unique_id=new_unique_id, data=new_data
)
2 changes: 1 addition & 1 deletion homeassistant/components/daikin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"iot_class": "local_polling",
"loggers": ["pydaikin"],
"quality_scale": "platinum",
"requirements": ["pydaikin==2.9.0"],
"requirements": ["pydaikin==2.10.5"],
"zeroconf": ["_dkapi._tcp.local."]
}
2 changes: 1 addition & 1 deletion homeassistant/components/daikin/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def async_setup_entry(
[
DaikinZoneSwitch(daikin_api, zone_id)
for zone_id, zone in enumerate(zones)
if zone != ("-", "0")
if zone[0] != "-"
]
)
if daikin_api.device.support_advanced_modes:
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ pycsspeechtts==1.0.8
# pycups==1.9.73

# homeassistant.components.daikin
pydaikin==2.9.0
pydaikin==2.10.5

# homeassistant.components.danfoss_air
pydanfossair==0.1.0
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ pycoolmasternet-async==0.1.5
pycsspeechtts==1.0.8

# homeassistant.components.daikin
pydaikin==2.9.0
pydaikin==2.10.5

# homeassistant.components.deconz
pydeconz==113
Expand Down