Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Tesla Wall Connector integration."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from datetime import timedelta
7 import logging
8 
9 from tesla_wall_connector import WallConnector
10 from tesla_wall_connector.exceptions import (
11  WallConnectorConnectionError,
12  WallConnectorConnectionTimeoutError,
13  WallConnectorError,
14 )
15 
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import CONF_HOST, CONF_SCAN_INTERVAL, Platform
18 from homeassistant.core import HomeAssistant
19 from homeassistant.exceptions import ConfigEntryNotReady
20 from homeassistant.helpers.aiohttp_client import async_get_clientsession
21 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
22 
23 from .const import (
24  DEFAULT_SCAN_INTERVAL,
25  DOMAIN,
26  WALLCONNECTOR_DATA_LIFETIME,
27  WALLCONNECTOR_DATA_VITALS,
28 )
29 
30 PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
31 
32 _LOGGER = logging.getLogger(__name__)
33 
34 
35 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
36  """Set up Tesla Wall Connector from a config entry."""
37  hass.data.setdefault(DOMAIN, {})
38  hostname = entry.data[CONF_HOST]
39 
40  wall_connector = WallConnector(host=hostname, session=async_get_clientsession(hass))
41 
42  try:
43  version_data = await wall_connector.async_get_version()
44  except WallConnectorError as ex:
45  raise ConfigEntryNotReady from ex
46 
47  async def async_update_data():
48  """Fetch new data from the Wall Connector."""
49  try:
50  vitals = await wall_connector.async_get_vitals()
51  lifetime = await wall_connector.async_get_lifetime()
52  except WallConnectorConnectionTimeoutError as ex:
53  raise UpdateFailed(
54  f"Could not fetch data from Tesla WallConnector at {hostname}: Timeout"
55  ) from ex
56  except WallConnectorConnectionError as ex:
57  raise UpdateFailed(
58  f"Could not fetch data from Tesla WallConnector at {hostname}: Cannot"
59  " connect"
60  ) from ex
61  except WallConnectorError as ex:
62  raise UpdateFailed(
63  f"Could not fetch data from Tesla WallConnector at {hostname}: {ex}"
64  ) from ex
65 
66  return {
67  WALLCONNECTOR_DATA_VITALS: vitals,
68  WALLCONNECTOR_DATA_LIFETIME: lifetime,
69  }
70 
71  coordinator: DataUpdateCoordinator = DataUpdateCoordinator(
72  hass,
73  _LOGGER,
74  config_entry=entry,
75  name="tesla-wallconnector",
76  update_interval=get_poll_interval(entry),
77  update_method=async_update_data,
78  )
79 
80  await coordinator.async_config_entry_first_refresh()
81 
82  hass.data[DOMAIN][entry.entry_id] = WallConnectorData(
83  wall_connector_client=wall_connector,
84  hostname=hostname,
85  part_number=version_data.part_number,
86  firmware_version=version_data.firmware_version,
87  serial_number=version_data.serial_number,
88  update_coordinator=coordinator,
89  )
90 
91  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
92 
93  entry.async_on_unload(entry.add_update_listener(update_listener))
94 
95  return True
96 
97 
98 def get_poll_interval(entry: ConfigEntry) -> timedelta:
99  """Get the poll interval from config."""
100  return timedelta(
101  seconds=entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
102  )
103 
104 
105 async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
106  """Handle options update."""
107  wall_connector_data: WallConnectorData = hass.data[DOMAIN][entry.entry_id]
108  wall_connector_data.update_coordinator.update_interval = get_poll_interval(entry)
109 
110 
111 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
112  """Unload a config entry."""
113  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
114  hass.data[DOMAIN].pop(entry.entry_id)
115 
116  return unload_ok
117 
118 
119 @dataclass
121  """Data for the Tesla Wall Connector integration."""
122 
123  wall_connector_client: WallConnector
124  update_coordinator: DataUpdateCoordinator
125  hostname: str
126  part_number: str
127  firmware_version: str
128  serial_number: str
timedelta get_poll_interval(ConfigEntry entry)
Definition: __init__.py:98
None update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:105
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:111
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:35
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)