core/homeassistant/components/livisi/__init__.py

61 lines
2.1 KiB
Python
Raw Normal View History

2022-11-07 13:40:23 +00:00
"""The Livisi Smart Home integration."""
2022-11-07 13:40:23 +00:00
from __future__ import annotations
from typing import Final
from aiohttp import ClientConnectorError
from aiolivisi import AioLivisi
from homeassistant import core
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
2022-11-07 13:40:23 +00:00
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client, device_registry as dr
from .const import DOMAIN
2022-11-07 13:40:23 +00:00
from .coordinator import LivisiDataUpdateCoordinator
Add livisi window sensor (WDS) (#90220) * Added support for livisi window sensor * Add const strings * added postpix for device_id * Remove unnecessary import * Fix imports * Fix lint errors, remove redundant device class property * Format code * Update .coveragerc * Finish basic window door sensor support * currently, only one binary sensor (wds) is supported * Remove unused imports * Fix isort issue * Simplify code as suggested in PR * rename get_device_response to get_device_state * fix ruff issue * Be more defensive in interpreting what we get from aiolivisi * Simplify coordinator * remove window sensor specific code (isOpen) * parameter order, type hinta * Update homeassistant/components/livisi/coordinator.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/livisi/coordinator.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/livisi/coordinator.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/livisi/binary_sensor.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/livisi/binary_sensor.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/livisi/binary_sensor.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --------- Co-authored-by: Tecotix <78791840+Tecotix@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
2023-03-24 13:52:50 +00:00
PLATFORMS: Final = [Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.SWITCH]
2022-11-07 13:40:23 +00:00
async def async_setup_entry(hass: core.HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Livisi Smart Home from a config entry."""
web_session = aiohttp_client.async_get_clientsession(hass)
aiolivisi = AioLivisi(web_session)
coordinator = LivisiDataUpdateCoordinator(hass, entry, aiolivisi)
try:
await coordinator.async_setup()
await coordinator.async_set_all_rooms()
except ClientConnectorError as exception:
raise ConfigEntryNotReady from exception
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
device_registry = dr.async_get(hass)
device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
2022-11-07 13:40:23 +00:00
identifiers={(DOMAIN, entry.entry_id)},
manufacturer="Livisi",
name=f"SHC {coordinator.controller_type} {coordinator.serial_number}",
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
await coordinator.async_config_entry_first_refresh()
2023-02-17 19:57:00 +00:00
entry.async_create_background_task(
hass, coordinator.ws_connect(), "livisi-ws_connect"
)
2022-11-07 13:40:23 +00:00
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
unload_success = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
await coordinator.websocket.disconnect()
if unload_success:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_success