78 lines
2.1 KiB
Python
78 lines
2.1 KiB
Python
"""The LocalTuya integration integration."""
|
|
import asyncio
|
|
import logging
|
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.const import (
|
|
CONF_PLATFORM,
|
|
CONF_ENTITIES,
|
|
)
|
|
|
|
from .const import DOMAIN, TUYA_DEVICE
|
|
from .config_flow import config_schema
|
|
from .common import TuyaDevice
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
UNSUB_LISTENER = "unsub_listener"
|
|
|
|
CONFIG_SCHEMA = config_schema()
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
"""Set up the LocalTuya integration component."""
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
for host_config in config.get(DOMAIN, []):
|
|
hass.async_create_task(
|
|
hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=host_config
|
|
)
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
"""Set up LocalTuya integration from a config entry."""
|
|
unsub_listener = entry.add_update_listener(update_listener)
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
|
UNSUB_LISTENER: unsub_listener,
|
|
TUYA_DEVICE: TuyaDevice(entry.data),
|
|
}
|
|
|
|
for entity in entry.data[CONF_ENTITIES]:
|
|
platform = entity[CONF_PLATFORM]
|
|
hass.async_create_task(
|
|
hass.config_entries.async_forward_entry_setup(entry, platform)
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
"""Unload a config entry."""
|
|
unload_ok = all(
|
|
await asyncio.gather(
|
|
*[
|
|
hass.config_entries.async_forward_entry_unload(entry, component)
|
|
for component in set(
|
|
entity[CONF_PLATFORM] for entity in entry.data[CONF_ENTITIES]
|
|
)
|
|
]
|
|
)
|
|
)
|
|
|
|
hass.data[DOMAIN][entry.entry_id][UNSUB_LISTENER]()
|
|
if unload_ok:
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
return True
|
|
|
|
|
|
async def update_listener(hass, config_entry):
|
|
"""Update listener."""
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|