Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Toon van Eneco devices."""
2 
3 import voluptuous as vol
4 
5 from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
6 from homeassistant.const import (
7  CONF_CLIENT_ID,
8  CONF_CLIENT_SECRET,
9  CONF_SCAN_INTERVAL,
10  EVENT_HOMEASSISTANT_STARTED,
11  Platform,
12 )
13 from homeassistant.core import CoreState, HomeAssistant
14 from homeassistant.helpers import config_validation as cv, device_registry as dr
16  OAuth2Session,
17  async_get_config_entry_implementation,
18 )
19 from homeassistant.helpers.typing import ConfigType
20 
21 from .const import CONF_AGREEMENT_ID, CONF_MIGRATE, DEFAULT_SCAN_INTERVAL, DOMAIN
22 from .coordinator import ToonDataUpdateCoordinator
23 from .oauth2 import register_oauth2_implementations
24 
25 PLATFORMS = [
26  Platform.BINARY_SENSOR,
27  Platform.CLIMATE,
28  Platform.SENSOR,
29  Platform.SWITCH,
30 ]
31 
32 # Validation of the user's configuration
33 CONFIG_SCHEMA = vol.Schema(
34  {
35  DOMAIN: vol.All(
36  cv.deprecated(CONF_SCAN_INTERVAL),
37  vol.Schema(
38  {
39  vol.Required(CONF_CLIENT_ID): cv.string,
40  vol.Required(CONF_CLIENT_SECRET): cv.string,
41  vol.Optional(
42  CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL
43  ): cv.positive_time_period,
44  }
45  ),
46  )
47  },
48  extra=vol.ALLOW_EXTRA,
49 )
50 
51 
52 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
53  """Set up the Toon components."""
54  if DOMAIN not in config:
55  return True
56 
58  hass, config[DOMAIN][CONF_CLIENT_ID], config[DOMAIN][CONF_CLIENT_SECRET]
59  )
60 
61  hass.async_create_task(
62  hass.config_entries.flow.async_init(DOMAIN, context={"source": SOURCE_IMPORT})
63  )
64 
65  return True
66 
67 
68 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
69  """Handle migration of a previous version config entry."""
70  if entry.version == 1:
71  # There is no usable data in version 1 anymore.
72  # The integration switched to OAuth and because of this, uses
73  # different unique identifiers as well.
74  # Force this by removing the existing entry and trigger a new flow.
75  hass.async_create_task(
76  hass.config_entries.flow.async_init(
77  DOMAIN,
78  context={"source": SOURCE_IMPORT},
79  data={CONF_MIGRATE: entry.entry_id},
80  )
81  )
82  return False
83 
84  return True
85 
86 
87 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
88  """Set up Toon from a config entry."""
89  implementation = await async_get_config_entry_implementation(hass, entry)
90  session = OAuth2Session(hass, entry, implementation)
91 
92  coordinator = ToonDataUpdateCoordinator(hass, entry=entry, session=session)
93  await coordinator.toon.activate_agreement(
94  agreement_id=entry.data[CONF_AGREEMENT_ID]
95  )
96  await coordinator.async_config_entry_first_refresh()
97 
98  hass.data.setdefault(DOMAIN, {})
99  hass.data[DOMAIN][entry.entry_id] = coordinator
100 
101  # Register device for the Meter Adapter, since it will have no entities.
102  device_registry = dr.async_get(hass)
103  device_registry.async_get_or_create(
104  config_entry_id=entry.entry_id,
105  identifiers={
106  (
107  DOMAIN,
108  coordinator.data.agreement.agreement_id,
109  "meter_adapter",
110  ) # type: ignore[arg-type]
111  },
112  manufacturer="Eneco",
113  name="Meter Adapter",
114  via_device=(DOMAIN, coordinator.data.agreement.agreement_id),
115  )
116 
117  # Spin up the platforms
118  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
119 
120  # If Home Assistant is already in a running state, register the webhook
121  # immediately, else trigger it after Home Assistant has finished starting.
122  if hass.state is CoreState.running:
123  await coordinator.register_webhook()
124  else:
125  hass.bus.async_listen_once(
126  EVENT_HOMEASSISTANT_STARTED, coordinator.register_webhook
127  )
128 
129  return True
130 
131 
132 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
133  """Unload Toon config entry."""
134 
135  # Remove webhooks registration
136  await hass.data[DOMAIN][entry.entry_id].unregister_webhook()
137 
138  # Unload entities for this entry/device.
139  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
140 
141  # Cleanup
142  if unload_ok:
143  del hass.data[DOMAIN][entry.entry_id]
144 
145  return unload_ok
None register_oauth2_implementations(HomeAssistant hass, str client_id, str client_secret)
Definition: oauth2.py:16
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:87
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:132
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:68
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:52
AbstractOAuth2Implementation async_get_config_entry_implementation(HomeAssistant hass, config_entries.ConfigEntry config_entry)