forked from SteinRobert/homeassistant-vorwerk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.py
106 lines (83 loc) · 2.95 KB
/
switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Support for Vorwerk Connected Vacuums switches."""
import logging
from pybotvac.exceptions import NeatoRobotException
from pybotvac.robot import Robot
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from . import VorwerkState
from .const import (
VORWERK_DOMAIN,
VORWERK_ROBOT_API,
VORWERK_ROBOT_COORDINATOR,
VORWERK_ROBOTS,
)
_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 = [
VorwerkScheduleSwitch(
robot[VORWERK_ROBOT_API], robot[VORWERK_ROBOT_COORDINATOR]
)
for robot in hass.data[VORWERK_DOMAIN][entry.entry_id][VORWERK_ROBOTS]
]
if not dev:
return
async_add_entities(dev, True)
class VorwerkScheduleSwitch(CoordinatorEntity, ToggleEntity):
"""Vorwerk Schedule Switches."""
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
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."""
return self._state.available
@property
def unique_id(self):
"""Return a unique ID."""
return self._robot_serial
@property
def is_on(self):
"""Return true if switch is on."""
return bool(self._state.schedule_enabled)
@property
def device_info(self):
"""Device info for robot."""
return self._state.device_info
async def async_turn_on(self, **kwargs):
"""Turn the switch on."""
def turn_on():
try:
self.robot.enable_schedule()
except NeatoRobotException as ex:
_LOGGER.error(
"Vorwerk switch connection error '%s': %s", self.entity_id, ex
)
await self.hass.async_add_executor_job(turn_on)
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs):
"""Turn the switch off."""
def turn_off():
try:
self.robot.disable_schedule()
except NeatoRobotException as ex:
_LOGGER.error(
"Vorwerk switch connection error '%s': %s", self.entity_id, ex
)
await self.hass.async_add_executor_job(turn_off)
await self.coordinator.async_request_refresh()