1 """The sma integration."""
3 from __future__
import annotations
5 from datetime
import timedelta
7 from typing
import TYPE_CHECKING
18 EVENT_HOMEASSISTANT_STOP,
28 DEFAULT_SCAN_INTERVAL,
34 PYSMA_REMOVE_LISTENER,
38 _LOGGER = logging.getLogger(__name__)
42 """Set up sma from a config entry."""
44 protocol =
"https" if entry.data[CONF_SSL]
else "http"
45 url = f
"{protocol}://{entry.data[CONF_HOST]}"
46 verify_ssl = entry.data[CONF_VERIFY_SSL]
47 group = entry.data[CONF_GROUP]
48 password = entry.data[CONF_PASSWORD]
51 sma = pysma.SMA(session, url, password, group)
55 sma_device_info = await sma.device_info()
57 sensor_def = await sma.get_sensors()
59 pysma.exceptions.SmaReadException,
60 pysma.exceptions.SmaConnectionException,
62 raise ConfigEntryNotReady
from exc
65 assert entry.unique_id
69 configuration_url=url,
70 identifiers={(DOMAIN, entry.unique_id)},
71 manufacturer=sma_device_info[
"manufacturer"],
72 model=sma_device_info[
"type"],
73 name=sma_device_info[
"name"],
74 sw_version=sma_device_info[
"sw_version"],
78 async
def async_update_data():
79 """Update the used SMA sensors."""
81 await sma.read(sensor_def)
83 pysma.exceptions.SmaReadException,
84 pysma.exceptions.SmaConnectionException,
89 seconds=entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
97 update_method=async_update_data,
98 update_interval=interval,
102 await coordinator.async_config_entry_first_refresh()
103 except ConfigEntryNotReady:
104 await sma.close_session()
108 async
def async_close_session(event):
109 """Close the session."""
110 await sma.close_session()
112 remove_stop_listener = hass.bus.async_listen_once(
113 EVENT_HOMEASSISTANT_STOP, async_close_session
116 hass.data.setdefault(DOMAIN, {})
117 hass.data[DOMAIN][entry.entry_id] = {
119 PYSMA_COORDINATOR: coordinator,
120 PYSMA_SENSORS: sensor_def,
121 PYSMA_REMOVE_LISTENER: remove_stop_listener,
122 PYSMA_DEVICE_INFO: device_info,
125 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
131 """Unload a config entry."""
132 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
134 data = hass.data[DOMAIN].pop(entry.entry_id)
135 await data[PYSMA_OBJECT].close_session()
136 data[PYSMA_REMOVE_LISTENER]()
144 _LOGGER.debug(
"Migrating from version %s", entry.version)
146 if entry.version == 1:
148 if entry.minor_version == 1:
150 hass.config_entries.async_update_entry(
151 entry, unique_id=
str(entry.unique_id), minor_version=minor_version
154 _LOGGER.debug(
"Migration successful")
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
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)