Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The P1 Monitor integration."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import CONF_HOST, CONF_PORT, Platform
7 from homeassistant.core import HomeAssistant
8 from homeassistant.exceptions import ConfigEntryNotReady
9 
10 from .const import LOGGER
11 from .coordinator import P1MonitorDataUpdateCoordinator
12 
13 PLATFORMS: list[Platform] = [Platform.SENSOR]
14 
15 type P1MonitorConfigEntry = ConfigEntry[P1MonitorDataUpdateCoordinator]
16 
17 
18 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
19  """Set up P1 Monitor from a config entry."""
20 
21  coordinator = P1MonitorDataUpdateCoordinator(hass)
22  try:
23  await coordinator.async_config_entry_first_refresh()
24  except ConfigEntryNotReady:
25  await coordinator.p1monitor.close()
26  raise
27 
28  entry.runtime_data = coordinator
29 
30  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
31  return True
32 
33 
34 async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
35  """Migrate old entry."""
36  LOGGER.debug("Migrating from version %s", config_entry.version)
37 
38  if config_entry.version == 1:
39  # Migrate to split host and port
40  host = config_entry.data[CONF_HOST]
41  if ":" in host:
42  host, port = host.split(":")
43  else:
44  port = 80
45 
46  new_data = {
47  **config_entry.data,
48  CONF_HOST: host,
49  CONF_PORT: int(port),
50  }
51 
52  hass.config_entries.async_update_entry(config_entry, data=new_data, version=2)
53  LOGGER.debug("Migration to version %s successful", config_entry.version)
54  return True
55 
56 
57 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
58  """Unload P1 Monitor config entry."""
59  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_migrate_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:34
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:57
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:18