2021-02-22 18:53:57 +00:00
|
|
|
"""The Litter-Robot integration."""
|
2022-07-25 20:52:13 +00:00
|
|
|
from __future__ import annotations
|
2021-02-22 18:53:57 +00:00
|
|
|
|
2022-12-22 16:22:21 +00:00
|
|
|
from pylitterbot import FeederRobot, LitterRobot, LitterRobot3, LitterRobot4, Robot
|
2022-08-31 10:54:59 +00:00
|
|
|
|
2021-02-22 18:53:57 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-06 03:10:07 +00:00
|
|
|
from homeassistant.const import Platform
|
2021-02-22 18:53:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .hub import LitterRobotHub
|
|
|
|
|
2022-08-31 10:54:59 +00:00
|
|
|
PLATFORMS_BY_TYPE = {
|
2022-09-02 20:18:10 +00:00
|
|
|
Robot: (
|
2022-09-26 02:40:34 +00:00
|
|
|
Platform.BINARY_SENSOR,
|
2022-08-31 10:54:59 +00:00
|
|
|
Platform.SELECT,
|
|
|
|
Platform.SENSOR,
|
|
|
|
Platform.SWITCH,
|
|
|
|
),
|
2022-09-02 20:18:10 +00:00
|
|
|
LitterRobot: (Platform.VACUUM,),
|
|
|
|
LitterRobot3: (Platform.BUTTON,),
|
2022-12-22 16:22:21 +00:00
|
|
|
LitterRobot4: (Platform.UPDATE,),
|
2022-09-02 20:18:10 +00:00
|
|
|
FeederRobot: (Platform.BUTTON,),
|
2022-08-31 10:54:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 18:53:57 +00:00
|
|
|
|
2022-09-02 20:18:10 +00:00
|
|
|
def get_platforms_for_robots(robots: list[Robot]) -> set[Platform]:
|
|
|
|
"""Get platforms for robots."""
|
|
|
|
return {
|
|
|
|
platform
|
|
|
|
for robot in robots
|
|
|
|
for robot_type, platforms in PLATFORMS_BY_TYPE.items()
|
|
|
|
if isinstance(robot, robot_type)
|
|
|
|
for platform in platforms
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-02-22 18:53:57 +00:00
|
|
|
"""Set up Litter-Robot from a config entry."""
|
2021-04-16 16:23:27 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2021-02-22 18:53:57 +00:00
|
|
|
hub = hass.data[DOMAIN][entry.entry_id] = LitterRobotHub(hass, entry.data)
|
2022-09-17 09:29:56 +00:00
|
|
|
await hub.login(load_robots=True, subscribe_for_updates=True)
|
2021-02-22 18:53:57 +00:00
|
|
|
|
2022-09-02 20:18:10 +00:00
|
|
|
if platforms := get_platforms_for_robots(hub.account.robots):
|
2022-08-31 10:54:59 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, platforms)
|
2021-02-22 18:53:57 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-02-22 18:53:57 +00:00
|
|
|
"""Unload a config entry."""
|
2022-08-25 16:32:27 +00:00
|
|
|
hub: LitterRobotHub = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
await hub.account.disconnect()
|
|
|
|
|
2022-09-02 20:18:10 +00:00
|
|
|
platforms = get_platforms_for_robots(hub.account.robots)
|
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, platforms)
|
|
|
|
|
2021-02-22 18:53:57 +00:00
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|