* Device Tracker works * Device Tracker works * Binary Sensor * Sensor * Lock * Switch and service * New switches * Update interval options * WIP * Translation errors * Check online state * WIP * Move to aiohttp * Some checks * CI * CI * .coveragerc * Black * icon_for_signal_level test * update_interval renamed to scan_interval * async logic * Fix cookie read * Requirement starline * Reformat * Requirements updated * ConfigEntryNotReady * Requirement starline * Lint fix * Requirement starline * available status * Translations * Expiration to config * CI * Linter fix * Small renaming * Update slnet token * Starline version bump * Fix updates * Black * Small fix * Removed unused fields * CI * set_scan_interval service * deps updated * Horn switch * Starline lib updated * Starline lib updated * Black * Support multiple integrations * Review * async_will_remove_from_hass * Deps updated * Test config flow * Requirements * CI * Review * Review * Review * Review * Review * CI * pylint fix * Review * Support "mayak" devices * Icons removed * Removed options_flow * Removed options_flow test * Removed options_flow test
86 lines
2.8 KiB
Python
86 lines
2.8 KiB
Python
"""The StarLine component."""
|
|
import voluptuous as vol
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import Config, HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
from .account import StarlineAccount
|
|
from .const import (
|
|
DOMAIN,
|
|
PLATFORMS,
|
|
SERVICE_UPDATE_STATE,
|
|
SERVICE_SET_SCAN_INTERVAL,
|
|
CONF_SCAN_INTERVAL,
|
|
DEFAULT_SCAN_INTERVAL,
|
|
)
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: Config) -> bool:
|
|
"""Set up configured StarLine."""
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
|
"""Set up the StarLine device from a config entry."""
|
|
account = StarlineAccount(hass, config_entry)
|
|
await account.update()
|
|
if not account.api.available:
|
|
raise ConfigEntryNotReady
|
|
|
|
if DOMAIN not in hass.data:
|
|
hass.data[DOMAIN] = {}
|
|
hass.data[DOMAIN][config_entry.entry_id] = account
|
|
|
|
device_registry = await hass.helpers.device_registry.async_get_registry()
|
|
for device in account.api.devices.values():
|
|
device_registry.async_get_or_create(
|
|
config_entry_id=config_entry.entry_id, **account.device_info(device)
|
|
)
|
|
|
|
for domain in PLATFORMS:
|
|
hass.async_create_task(
|
|
hass.config_entries.async_forward_entry_setup(config_entry, domain)
|
|
)
|
|
|
|
async def async_set_scan_interval(call):
|
|
"""Service for set scan interval."""
|
|
options = dict(config_entry.options)
|
|
options[CONF_SCAN_INTERVAL] = call.data[CONF_SCAN_INTERVAL]
|
|
hass.config_entries.async_update_entry(entry=config_entry, options=options)
|
|
|
|
hass.services.async_register(DOMAIN, SERVICE_UPDATE_STATE, account.update)
|
|
hass.services.async_register(
|
|
DOMAIN,
|
|
SERVICE_SET_SCAN_INTERVAL,
|
|
async_set_scan_interval,
|
|
schema=vol.Schema(
|
|
{
|
|
vol.Required(CONF_SCAN_INTERVAL): vol.All(
|
|
vol.Coerce(int), vol.Range(min=10)
|
|
)
|
|
}
|
|
),
|
|
)
|
|
|
|
config_entry.add_update_listener(async_options_updated)
|
|
await async_options_updated(hass, config_entry)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
for domain in PLATFORMS:
|
|
await hass.config_entries.async_forward_entry_unload(config_entry, domain)
|
|
|
|
account: StarlineAccount = hass.data[DOMAIN][config_entry.entry_id]
|
|
account.unload()
|
|
return True
|
|
|
|
|
|
async def async_options_updated(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
|
"""Triggered by config entry options updates."""
|
|
account: StarlineAccount = hass.data[DOMAIN][config_entry.entry_id]
|
|
scan_interval = config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
|
account.set_update_interval(scan_interval)
|