Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Fujitsu HVAC (based on Ayla IOT) integration."""
2 
3 from __future__ import annotations
4 
5 from contextlib import suppress
6 
7 from ayla_iot_unofficial import new_ayla_api
8 from ayla_iot_unofficial.fujitsu_consts import FGLAIR_APP_CREDENTIALS
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers import aiohttp_client
14 
15 from .const import API_TIMEOUT, CONF_EUROPE, CONF_REGION, REGION_DEFAULT, REGION_EU
16 from .coordinator import FGLairCoordinator
17 
18 PLATFORMS: list[Platform] = [Platform.CLIMATE]
19 
20 type FGLairConfigEntry = ConfigEntry[FGLairCoordinator]
21 
22 
23 async def async_setup_entry(hass: HomeAssistant, entry: FGLairConfigEntry) -> bool:
24  """Set up Fujitsu HVAC (based on Ayla IOT) from a config entry."""
25  app_id, app_secret = FGLAIR_APP_CREDENTIALS[entry.data[CONF_REGION]]
26  api = new_ayla_api(
27  entry.data[CONF_USERNAME],
28  entry.data[CONF_PASSWORD],
29  app_id,
30  app_secret,
31  europe=entry.data[CONF_REGION] == REGION_EU,
32  websession=aiohttp_client.async_get_clientsession(hass),
33  timeout=API_TIMEOUT,
34  )
35 
36  coordinator = FGLairCoordinator(hass, api)
37  await coordinator.async_config_entry_first_refresh()
38 
39  entry.runtime_data = coordinator
40 
41  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
42  return True
43 
44 
45 async def async_unload_entry(hass: HomeAssistant, entry: FGLairConfigEntry) -> bool:
46  """Unload a config entry."""
47  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
48  with suppress(TimeoutError):
49  await entry.runtime_data.api.async_sign_out()
50 
51  return unload_ok
52 
53 
54 async def async_migrate_entry(hass: HomeAssistant, entry: FGLairConfigEntry) -> bool:
55  """Migrate old entry."""
56  if entry.version > 1:
57  return False
58 
59  if entry.version == 1:
60  new_data = {**entry.data}
61  if entry.minor_version < 2:
62  is_europe = new_data.get(CONF_EUROPE, False)
63  if is_europe:
64  new_data[CONF_REGION] = REGION_EU
65  else:
66  new_data[CONF_REGION] = REGION_DEFAULT
67 
68  hass.config_entries.async_update_entry(
69  entry, data=new_data, minor_version=2, version=1
70  )
71 
72  return True
bool async_migrate_entry(HomeAssistant hass, FGLairConfigEntry entry)
Definition: __init__.py:54
bool async_setup_entry(HomeAssistant hass, FGLairConfigEntry entry)
Definition: __init__.py:23
bool async_unload_entry(HomeAssistant hass, FGLairConfigEntry entry)
Definition: __init__.py:45