Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The A. O. Smith integration."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from py_aosmith import AOSmithAPIClient
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import aiohttp_client, device_registry as dr
13 
14 from .const import DOMAIN
15 from .coordinator import AOSmithEnergyCoordinator, AOSmithStatusCoordinator
16 
17 PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.WATER_HEATER]
18 
19 type AOSmithConfigEntry = ConfigEntry[AOSmithData]
20 
21 
22 @dataclass
24  """Data for the A. O. Smith integration."""
25 
26  client: AOSmithAPIClient
27  status_coordinator: AOSmithStatusCoordinator
28  energy_coordinator: AOSmithEnergyCoordinator
29 
30 
31 async def async_setup_entry(hass: HomeAssistant, entry: AOSmithConfigEntry) -> bool:
32  """Set up A. O. Smith from a config entry."""
33  email = entry.data[CONF_EMAIL]
34  password = entry.data[CONF_PASSWORD]
35 
36  session = aiohttp_client.async_get_clientsession(hass)
37  client = AOSmithAPIClient(email, password, session)
38 
39  status_coordinator = AOSmithStatusCoordinator(hass, client)
40  await status_coordinator.async_config_entry_first_refresh()
41 
42  device_registry = dr.async_get(hass)
43  for junction_id, aosmith_device in status_coordinator.data.items():
44  device_registry.async_get_or_create(
45  config_entry_id=entry.entry_id,
46  identifiers={(DOMAIN, junction_id)},
47  manufacturer="A. O. Smith",
48  name=aosmith_device.name,
49  model=aosmith_device.model,
50  serial_number=aosmith_device.serial,
51  suggested_area=aosmith_device.install_location,
52  sw_version=aosmith_device.status.firmware_version,
53  )
54 
55  energy_coordinator = AOSmithEnergyCoordinator(
56  hass, client, list(status_coordinator.data)
57  )
58  await energy_coordinator.async_config_entry_first_refresh()
59 
60  entry.runtime_data = AOSmithData(
61  client,
62  status_coordinator,
63  energy_coordinator,
64  )
65 
66  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
67 
68  return True
69 
70 
71 async def async_unload_entry(hass: HomeAssistant, entry: AOSmithConfigEntry) -> bool:
72  """Unload a config entry."""
73  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, AOSmithConfigEntry entry)
Definition: __init__.py:71
bool async_setup_entry(HomeAssistant hass, AOSmithConfigEntry entry)
Definition: __init__.py:31