homeassistant-vorwerk/switch.py

107 lines
3.0 KiB
Python
Raw Normal View History

2021-03-20 16:11:04 +01:00
"""Support for Vorwerk Connected Vacuums switches."""
import logging
from pybotvac.exceptions import NeatoRobotException
2021-04-25 20:30:12 +02:00
from pybotvac.robot import Robot
2021-03-20 16:11:04 +01:00
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.helpers.entity import ToggleEntity
2021-04-25 20:30:12 +02:00
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from . import VorwerkState
2021-04-25 20:30:12 +02:00
from .const import (
VORWERK_DOMAIN,
VORWERK_ROBOT_API,
VORWERK_ROBOT_COORDINATOR,
VORWERK_ROBOTS,
)
2021-03-20 16:11:04 +01:00
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up Vorwerk switch with config entry."""
_LOGGER.debug("Adding switches for vorwerk (%s)", entry.title)
dev = [
2021-04-25 20:30:12 +02:00
VorwerkScheduleSwitch(
robot[VORWERK_ROBOT_API], robot[VORWERK_ROBOT_COORDINATOR]
)
2021-03-20 16:11:04 +01:00
for robot in hass.data[VORWERK_DOMAIN][entry.entry_id][VORWERK_ROBOTS]
]
if not dev:
return
async_add_entities(dev, True)
2021-04-25 20:30:12 +02:00
class VorwerkScheduleSwitch(CoordinatorEntity, ToggleEntity):
"""Vorwerk Schedule Switches."""
2021-03-20 16:11:04 +01:00
2021-04-25 20:30:12 +02:00
def __init__(
self, robot_state: VorwerkState, coordinator: DataUpdateCoordinator
) -> None:
"""Initialize the Vorwerk Schedule switch."""
super().__init__(coordinator)
self.robot: Robot = robot_state.robot
self._robot_name = f"{self.robot.name} Schedule"
self._state: VorwerkState = robot_state
2021-03-20 16:11:04 +01:00
self._robot_serial = self.robot.serial
@property
def name(self):
"""Return the name of the switch."""
return self._robot_name
@property
def available(self):
"""Return True if entity is available."""
2021-04-25 20:30:12 +02:00
return self._state.available
2021-03-20 16:11:04 +01:00
@property
def unique_id(self):
"""Return a unique ID."""
return self._robot_serial
@property
def is_on(self):
"""Return true if switch is on."""
2021-06-03 11:06:53 +02:00
return bool(self._state.schedule_enabled)
2021-03-20 16:11:04 +01:00
@property
def device_info(self):
"""Device info for robot."""
2021-04-25 20:30:12 +02:00
return self._state.device_info
2021-03-20 16:11:04 +01:00
2021-04-25 20:30:12 +02:00
async def async_turn_on(self, **kwargs):
2021-03-20 16:11:04 +01:00
"""Turn the switch on."""
2021-04-25 20:30:12 +02:00
def turn_on():
2021-03-20 16:11:04 +01:00
try:
self.robot.enable_schedule()
except NeatoRobotException as ex:
_LOGGER.error(
"Vorwerk switch connection error '%s': %s", self.entity_id, ex
)
2021-04-25 20:30:12 +02:00
await self.hass.async_add_executor_job(turn_on)
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs):
2021-03-20 16:11:04 +01:00
"""Turn the switch off."""
2021-04-25 20:30:12 +02:00
def turn_off():
2021-03-20 16:11:04 +01:00
try:
self.robot.disable_schedule()
except NeatoRobotException as ex:
_LOGGER.error(
"Vorwerk switch connection error '%s': %s", self.entity_id, ex
)
2021-04-25 20:30:12 +02:00
await self.hass.async_add_executor_job(turn_off)
await self.coordinator.async_request_refresh()