46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
"""The Schlage integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pycognito.exceptions import WarrantException
|
|
import pyschlage
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
|
|
|
from .const import DOMAIN
|
|
from .coordinator import SchlageDataUpdateCoordinator
|
|
|
|
PLATFORMS: list[Platform] = [
|
|
Platform.BINARY_SENSOR,
|
|
Platform.LOCK,
|
|
Platform.SENSOR,
|
|
Platform.SWITCH,
|
|
]
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Set up Schlage from a config entry."""
|
|
username = entry.data[CONF_USERNAME]
|
|
password = entry.data[CONF_PASSWORD]
|
|
try:
|
|
auth = await hass.async_add_executor_job(pyschlage.Auth, username, password)
|
|
except WarrantException as ex:
|
|
raise ConfigEntryAuthFailed from ex
|
|
|
|
coordinator = SchlageDataUpdateCoordinator(hass, username, pyschlage.Schlage(auth))
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
|
await coordinator.async_config_entry_first_refresh()
|
|
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
|