1 """Platform for the Daikin AC."""
3 from __future__
import annotations
8 from aiohttp
import ClientConnectionError
9 from pydaikin.daikin_base
import Appliance
10 from pydaikin.factory
import DaikinFactory
26 from .const
import DOMAIN, KEY_MAC, TIMEOUT
27 from .coordinator
import DaikinCoordinator
29 _LOGGER = logging.getLogger(__name__)
32 PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.SWITCH]
36 """Establish connection with Daikin."""
39 if entry.unique_id
is None or ".local" in entry.unique_id:
40 hass.config_entries.async_update_entry(entry, unique_id=conf[KEY_MAC])
43 host = conf[CONF_HOST]
45 async
with asyncio.timeout(TIMEOUT):
46 device: Appliance = await DaikinFactory(
49 key=entry.data.get(CONF_API_KEY),
50 uuid=entry.data.get(CONF_UUID),
51 password=entry.data.get(CONF_PASSWORD),
53 _LOGGER.debug(
"Connection to %s successful", host)
54 except TimeoutError
as err:
55 _LOGGER.debug(
"Connection to %s timed out in 60 seconds", host)
56 raise ConfigEntryNotReady
from err
57 except ClientConnectionError
as err:
58 _LOGGER.debug(
"ClientConnectionError to %s", host)
59 raise ConfigEntryNotReady
from err
63 await coordinator.async_config_entry_first_refresh()
67 hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
68 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
73 """Unload a config entry."""
74 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
76 hass.data[DOMAIN].pop(entry.entry_id)
77 if not hass.data[DOMAIN]:
83 hass: HomeAssistant, config_entry: ConfigEntry, device: Appliance
85 """Migrate old entry."""
86 dev_reg = dr.async_get(hass)
87 ent_reg = er.async_get(hass)
88 old_unique_id = config_entry.unique_id
89 new_unique_id = device.mac
90 new_mac = dr.format_mac(new_unique_id)
91 new_name = device.values.get(
"name",
"Daikin AC")
94 def _update_unique_id(entity_entry: er.RegistryEntry) -> dict[str, str] |
None:
95 """Update unique ID of entity entry."""
98 if new_unique_id == old_unique_id:
101 duplicate = dev_reg.async_get_device(
102 connections={(CONNECTION_NETWORK_MAC, new_mac)}, identifiers=
None
106 if duplicate
is not None:
107 if config_entry.entry_id
in duplicate.config_entries:
109 "Removing duplicated device %s",
116 duplicate_entities = er.async_entries_for_device(
117 ent_reg, duplicate.id,
True
119 for entity
in duplicate_entities:
120 if entity.config_entry_id == config_entry.entry_id:
121 ent_reg.async_remove(entity.entity_id)
123 dev_reg.async_update_device(
124 duplicate.id, remove_config_entry_id=config_entry.entry_id
128 for device_entry
in dr.async_entries_for_config_entry(
129 dev_reg, config_entry.entry_id
131 for connection
in device_entry.connections:
132 if connection[1] == old_unique_id:
133 new_connections = {(CONNECTION_NETWORK_MAC, new_mac)}
136 "Migrating device %s connections to %s",
140 dev_reg.async_update_device(
142 merge_connections=new_connections,
145 if device_entry.name
is None:
147 "Migrating device name to %s",
150 dev_reg.async_update_device(
156 await er.async_migrate_entries(hass, config_entry.entry_id, _update_unique_id)
158 new_data = {**config_entry.data, KEY_MAC: dr.format_mac(new_unique_id)}
160 hass.config_entries.async_update_entry(
161 config_entry, unique_id=new_unique_id, data=new_data
167 entity_entry: er.RegistryEntry, unique_id: str
168 ) -> dict[str, str] |
None:
169 """Update unique ID of entity entry."""
170 if entity_entry.unique_id.startswith(unique_id):
174 unique_id_parts = entity_entry.unique_id.split(
"-")
175 unique_id_parts[0] = unique_id
176 entity_new_unique_id =
"-".join(unique_id_parts)
179 "Migrating entity %s from %s to new id %s",
180 entity_entry.entity_id,
181 entity_entry.unique_id,
182 entity_new_unique_id,
184 return {
"new_unique_id": entity_new_unique_id}
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
None async_migrate_unique_id(HomeAssistant hass, ConfigEntry config_entry, Appliance device)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
dict[str, str]|None update_unique_id(er.RegistryEntry entity_entry, str unique_id)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)