63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
"""The SwitchBee Smart Home integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from switchbee.api import CentralUnitAPI, SwitchBeeError
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .const import DOMAIN
|
|
from .coordinator import SwitchBeeCoordinator
|
|
|
|
PLATFORMS: list[Platform] = [
|
|
Platform.BUTTON,
|
|
Platform.CLIMATE,
|
|
Platform.COVER,
|
|
Platform.LIGHT,
|
|
Platform.SWITCH,
|
|
]
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Set up SwitchBee Smart Home from a config entry."""
|
|
hass.data.setdefault(DOMAIN, {})
|
|
central_unit = entry.data[CONF_HOST]
|
|
user = entry.data[CONF_USERNAME]
|
|
password = entry.data[CONF_PASSWORD]
|
|
websession = async_get_clientsession(hass, verify_ssl=False)
|
|
api = CentralUnitAPI(central_unit, user, password, websession)
|
|
try:
|
|
await api.connect()
|
|
except SwitchBeeError as exp:
|
|
raise ConfigEntryNotReady("Failed to connect to the Central Unit") from exp
|
|
|
|
coordinator = SwitchBeeCoordinator(
|
|
hass,
|
|
api,
|
|
)
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
entry.async_on_unload(entry.add_update_listener(update_listener))
|
|
hass.data[DOMAIN][entry.entry_id] = coordinator
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
return unload_ok
|
|
|
|
|
|
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
|
"""Update listener."""
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|