59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
"""A wrapper 'hub' for the Litter-Robot API."""
|
|
from datetime import timedelta
|
|
import logging
|
|
|
|
from pylitterbot import Account
|
|
from pylitterbot.exceptions import LitterRobotException, LitterRobotLoginException
|
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|
|
|
from .const import DOMAIN
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
UPDATE_INTERVAL_SECONDS = 20
|
|
|
|
|
|
class LitterRobotHub:
|
|
"""A Litter-Robot hub wrapper class."""
|
|
|
|
def __init__(self, hass: HomeAssistant, data: dict) -> None:
|
|
"""Initialize the Litter-Robot hub."""
|
|
self._data = data
|
|
self.account = None
|
|
self.logged_in = False
|
|
|
|
async def _async_update_data() -> bool:
|
|
"""Update all device states from the Litter-Robot API."""
|
|
await self.account.refresh_robots()
|
|
return True
|
|
|
|
self.coordinator = DataUpdateCoordinator(
|
|
hass,
|
|
_LOGGER,
|
|
name=DOMAIN,
|
|
update_method=_async_update_data,
|
|
update_interval=timedelta(seconds=UPDATE_INTERVAL_SECONDS),
|
|
)
|
|
|
|
async def login(self, load_robots: bool = False) -> None:
|
|
"""Login to Litter-Robot."""
|
|
self.logged_in = False
|
|
self.account = Account()
|
|
try:
|
|
await self.account.connect(
|
|
username=self._data[CONF_USERNAME],
|
|
password=self._data[CONF_PASSWORD],
|
|
load_robots=load_robots,
|
|
)
|
|
self.logged_in = True
|
|
return self.logged_in
|
|
except LitterRobotLoginException as ex:
|
|
_LOGGER.error("Invalid credentials")
|
|
raise ex
|
|
except LitterRobotException as ex:
|
|
_LOGGER.error("Unable to connect to Litter-Robot API")
|
|
raise ex
|