Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Goal Zero Yeti integration."""
2 
3 from __future__ import annotations
4 
5 from typing import TYPE_CHECKING
6 
7 from goalzero import Yeti, exceptions
8 
9 from homeassistant.const import CONF_HOST, Platform
10 from homeassistant.core import HomeAssistant
11 from homeassistant.exceptions import ConfigEntryNotReady
12 from homeassistant.helpers.aiohttp_client import async_get_clientsession
13 from homeassistant.helpers.device_registry import format_mac
14 
15 from .coordinator import GoalZeroConfigEntry, GoalZeroDataUpdateCoordinator
16 
17 PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH]
18 
19 
20 async def async_setup_entry(hass: HomeAssistant, entry: GoalZeroConfigEntry) -> bool:
21  """Set up Goal Zero Yeti from a config entry."""
22 
23  mac = entry.unique_id
24 
25  if TYPE_CHECKING:
26  assert mac is not None
27 
28  if (formatted_mac := format_mac(mac)) != mac:
29  # The DHCP discovery path did not format the MAC address
30  # so we need to update the config entry if it's different
31  hass.config_entries.async_update_entry(entry, unique_id=formatted_mac)
32 
33  api = Yeti(entry.data[CONF_HOST], async_get_clientsession(hass))
34  try:
35  await api.init_connect()
36  except exceptions.ConnectError as ex:
37  raise ConfigEntryNotReady(f"Failed to connect to device: {ex}") from ex
38 
39  entry.runtime_data = GoalZeroDataUpdateCoordinator(hass, api)
40  await entry.runtime_data.async_config_entry_first_refresh()
41  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
42 
43  return True
44 
45 
46 async def async_unload_entry(hass: HomeAssistant, entry: GoalZeroConfigEntry) -> bool:
47  """Unload a config entry."""
48  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, GoalZeroConfigEntry entry)
Definition: __init__.py:46
bool async_setup_entry(HomeAssistant hass, GoalZeroConfigEntry entry)
Definition: __init__.py:20
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)