Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The SolarEdge integration."""
2 
3 from __future__ import annotations
4 
5 import socket
6 
7 from aiohttp import ClientError
8 from aiosolaredge import SolarEdge
9 
10 from homeassistant.const import CONF_API_KEY, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryNotReady
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import CONF_SITE_ID, LOGGER
16 from .types import SolarEdgeConfigEntry, SolarEdgeData
17 
18 PLATFORMS = [Platform.SENSOR]
19 
20 
21 async def async_setup_entry(hass: HomeAssistant, entry: SolarEdgeConfigEntry) -> bool:
22  """Set up SolarEdge from a config entry."""
23  session = async_get_clientsession(hass)
24  api = SolarEdge(entry.data[CONF_API_KEY], session)
25 
26  try:
27  response = await api.get_details(entry.data[CONF_SITE_ID])
28  except (TimeoutError, ClientError, socket.gaierror) as ex:
29  LOGGER.error("Could not retrieve details from SolarEdge API")
30  raise ConfigEntryNotReady from ex
31 
32  if "details" not in response:
33  LOGGER.error("Missing details data in SolarEdge response")
34  raise ConfigEntryNotReady
35 
36  if response["details"].get("status", "").lower() != "active":
37  LOGGER.error("SolarEdge site is not active")
38  return False
39 
40  entry.runtime_data = SolarEdgeData(api_client=api)
41  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
42  return True
43 
44 
45 async def async_unload_entry(hass: HomeAssistant, entry: SolarEdgeConfigEntry) -> bool:
46  """Unload SolarEdge config entry."""
47  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
bool async_setup_entry(HomeAssistant hass, SolarEdgeConfigEntry entry)
Definition: __init__.py:21
bool async_unload_entry(HomeAssistant hass, SolarEdgeConfigEntry entry)
Definition: __init__.py:45
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)