2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Neato Connected Vacuums switches."""
|
2021-08-08 13:02:37 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-06-21 01:46:15 +00:00
|
|
|
from datetime import timedelta
|
2019-03-21 05:56:46 +00:00
|
|
|
import logging
|
2021-08-08 13:02:37 +00:00
|
|
|
from typing import Any
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-10-06 18:10:11 +00:00
|
|
|
from pybotvac.exceptions import NeatoRobotException
|
2021-08-08 13:02:37 +00:00
|
|
|
from pybotvac.robot import Robot
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
from homeassistant.components.neato import NeatoHub
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2016-11-19 08:14:40 +00:00
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON
|
2021-08-08 13:02:37 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import DeviceInfo, ToggleEntity
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-10-06 18:10:11 +00:00
|
|
|
from .const import NEATO_DOMAIN, NEATO_LOGIN, NEATO_ROBOTS, SCAN_INTERVAL_MINUTES
|
2016-10-22 04:14:45 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-10-06 18:10:11 +00:00
|
|
|
SCAN_INTERVAL = timedelta(minutes=SCAN_INTERVAL_MINUTES)
|
2018-06-27 20:55:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SWITCH_TYPE_SCHEDULE = "schedule"
|
2016-10-22 04:14:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SWITCH_TYPES = {SWITCH_TYPE_SCHEDULE: ["Schedule"]}
|
2016-10-22 04:14:45 +00:00
|
|
|
|
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2019-10-06 11:05:51 +00:00
|
|
|
"""Set up Neato switch with config entry."""
|
2016-10-22 04:14:45 +00:00
|
|
|
dev = []
|
2021-08-08 13:02:37 +00:00
|
|
|
neato: NeatoHub = hass.data[NEATO_LOGIN]
|
|
|
|
|
2016-11-19 08:14:40 +00:00
|
|
|
for robot in hass.data[NEATO_ROBOTS]:
|
2016-10-22 04:14:45 +00:00
|
|
|
for type_name in SWITCH_TYPES:
|
2019-10-07 19:49:54 +00:00
|
|
|
dev.append(NeatoConnectedSwitch(neato, robot, type_name))
|
2019-10-06 11:05:51 +00:00
|
|
|
|
|
|
|
if not dev:
|
|
|
|
return
|
|
|
|
|
2017-04-24 03:41:09 +00:00
|
|
|
_LOGGER.debug("Adding switches %s", dev)
|
2019-10-06 11:05:51 +00:00
|
|
|
async_add_entities(dev, True)
|
2016-10-22 04:14:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NeatoConnectedSwitch(ToggleEntity):
|
2016-11-19 08:14:40 +00:00
|
|
|
"""Neato Connected Switches."""
|
2016-10-22 04:14:45 +00:00
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
def __init__(self, neato: NeatoHub, robot: Robot, switch_type: str) -> None:
|
2016-11-19 08:14:40 +00:00
|
|
|
"""Initialize the Neato Connected switches."""
|
2016-10-22 04:14:45 +00:00
|
|
|
self.type = switch_type
|
|
|
|
self.robot = robot
|
2021-01-13 09:23:16 +00:00
|
|
|
self._available = False
|
2019-10-06 18:10:11 +00:00
|
|
|
self._robot_name = f"{self.robot.name} {SWITCH_TYPES[self.type][0]}"
|
2021-08-08 13:02:37 +00:00
|
|
|
self._state: dict[str, Any] | None = None
|
|
|
|
self._schedule_state: str | None = None
|
2016-11-19 08:14:40 +00:00
|
|
|
self._clean_state = None
|
2021-08-08 13:02:37 +00:00
|
|
|
self._robot_serial: str = self.robot.serial
|
2016-10-22 04:14:45 +00:00
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
def update(self) -> None:
|
2016-11-19 08:14:40 +00:00
|
|
|
"""Update the states of Neato switches."""
|
2020-07-15 16:26:57 +00:00
|
|
|
_LOGGER.debug("Running Neato switch update for '%s'", self.entity_id)
|
2017-04-03 12:42:21 +00:00
|
|
|
try:
|
|
|
|
self._state = self.robot.state
|
2019-10-06 18:10:11 +00:00
|
|
|
except NeatoRobotException as ex:
|
2019-10-07 06:30:49 +00:00
|
|
|
if self._available: # Print only once when available
|
2020-07-15 16:26:57 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Neato switch connection error for '%s': %s", self.entity_id, ex
|
|
|
|
)
|
2017-04-03 12:42:21 +00:00
|
|
|
self._state = None
|
2019-10-07 06:30:49 +00:00
|
|
|
self._available = False
|
2017-04-03 12:42:21 +00:00
|
|
|
return
|
2019-10-06 18:10:11 +00:00
|
|
|
|
2019-10-07 19:49:54 +00:00
|
|
|
self._available = True
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("self._state=%s", self._state)
|
2016-11-19 08:14:40 +00:00
|
|
|
if self.type == SWITCH_TYPE_SCHEDULE:
|
2017-04-24 03:41:09 +00:00
|
|
|
_LOGGER.debug("State: %s", self._state)
|
2021-08-08 13:02:37 +00:00
|
|
|
if self._state is not None and self._state["details"]["isScheduleEnabled"]:
|
2016-11-19 08:14:40 +00:00
|
|
|
self._schedule_state = STATE_ON
|
|
|
|
else:
|
|
|
|
self._schedule_state = STATE_OFF
|
2020-07-15 16:26:57 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"Schedule state for '%s': %s", self.entity_id, self._schedule_state
|
|
|
|
)
|
2016-10-22 04:14:45 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def name(self) -> str:
|
2016-11-19 08:14:40 +00:00
|
|
|
"""Return the name of the switch."""
|
|
|
|
return self._robot_name
|
2016-10-22 04:14:45 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def available(self) -> bool:
|
2016-11-19 08:14:40 +00:00
|
|
|
"""Return True if entity is available."""
|
2019-10-07 06:30:49 +00:00
|
|
|
return self._available
|
2016-11-19 08:14:40 +00:00
|
|
|
|
2018-10-12 22:33:13 +00:00
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def unique_id(self) -> str:
|
2018-10-12 22:33:13 +00:00
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._robot_serial
|
|
|
|
|
2016-11-19 08:14:40 +00:00
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def is_on(self) -> bool:
|
2016-11-19 08:14:40 +00:00
|
|
|
"""Return true if switch is on."""
|
2021-08-08 13:02:37 +00:00
|
|
|
return bool(
|
|
|
|
self.type == SWITCH_TYPE_SCHEDULE and self._schedule_state == STATE_ON
|
|
|
|
)
|
2016-10-22 04:14:45 +00:00
|
|
|
|
2019-10-06 11:05:51 +00:00
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2019-10-06 11:05:51 +00:00
|
|
|
"""Device info for neato robot."""
|
|
|
|
return {"identifiers": {(NEATO_DOMAIN, self._robot_serial)}}
|
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2016-11-19 08:14:40 +00:00
|
|
|
"""Turn the switch on."""
|
2017-11-03 13:25:26 +00:00
|
|
|
if self.type == SWITCH_TYPE_SCHEDULE:
|
2019-10-06 18:10:11 +00:00
|
|
|
try:
|
|
|
|
self.robot.enable_schedule()
|
|
|
|
except NeatoRobotException as ex:
|
2020-07-15 16:26:57 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Neato switch connection error '%s': %s", self.entity_id, ex
|
|
|
|
)
|
2016-10-22 04:14:45 +00:00
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2016-11-19 08:14:40 +00:00
|
|
|
"""Turn the switch off."""
|
2017-11-03 13:25:26 +00:00
|
|
|
if self.type == SWITCH_TYPE_SCHEDULE:
|
2019-10-06 18:10:11 +00:00
|
|
|
try:
|
|
|
|
self.robot.disable_schedule()
|
|
|
|
except NeatoRobotException as ex:
|
2020-07-15 16:26:57 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Neato switch connection error '%s': %s", self.entity_id, ex
|
|
|
|
)
|