Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Obihai integration."""
2 
3 from homeassistant.config_entries import ConfigEntry
4 from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
5 from homeassistant.core import HomeAssistant
6 from homeassistant.helpers.device_registry import format_mac
7 
8 from .connectivity import ObihaiConnection
9 from .const import DOMAIN, LOGGER, PLATFORMS
10 
11 
12 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
13  """Set up from a config entry."""
14 
15  requester = ObihaiConnection(
16  entry.data[CONF_HOST],
17  username=entry.data[CONF_USERNAME],
18  password=entry.data[CONF_PASSWORD],
19  )
20  await hass.async_add_executor_job(requester.update)
21  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = requester
22  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
23 
24  return True
25 
26 
27 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
28  """Migrate old entry."""
29 
30  version = entry.version
31 
32  LOGGER.debug("Migrating from version %s", version)
33  if version != 2:
34  requester: ObihaiConnection = hass.data[DOMAIN][entry.entry_id]
35 
36  device_mac = await hass.async_add_executor_job(
37  requester.pyobihai.get_device_mac
38  )
39  hass.config_entries.async_update_entry(
40  entry, unique_id=format_mac(device_mac), version=2
41  )
42 
43  LOGGER.debug("Migration to version %s successful", entry.version)
44 
45  return True
46 
47 
48 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
49  """Unload a config entry."""
50  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:27
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:48
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:12