Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Neato Connected Vacuums switches."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 from typing import Any
8 
9 from pybotvac.exceptions import NeatoRobotException
10 from pybotvac.robot import Robot
11 
12 from homeassistant.components.switch import SwitchEntity
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import STATE_OFF, STATE_ON, EntityCategory
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import NEATO_LOGIN, NEATO_ROBOTS, SCAN_INTERVAL_MINUTES
19 from .entity import NeatoEntity
20 from .hub import NeatoHub
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 SCAN_INTERVAL = timedelta(minutes=SCAN_INTERVAL_MINUTES)
25 
26 SWITCH_TYPE_SCHEDULE = "schedule"
27 
28 SWITCH_TYPES = {SWITCH_TYPE_SCHEDULE: ["Schedule"]}
29 
30 
32  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
33 ) -> None:
34  """Set up Neato switch with config entry."""
35  neato: NeatoHub = hass.data[NEATO_LOGIN]
36  dev = [
37  NeatoConnectedSwitch(neato, robot, type_name)
38  for robot in hass.data[NEATO_ROBOTS]
39  for type_name in SWITCH_TYPES
40  ]
41 
42  if not dev:
43  return
44 
45  _LOGGER.debug("Adding switches %s", dev)
46  async_add_entities(dev, True)
47 
48 
50  """Neato Connected Switches."""
51 
52  _attr_translation_key = "schedule"
53  _attr_available = False
54  _attr_entity_category = EntityCategory.CONFIG
55 
56  def __init__(self, neato: NeatoHub, robot: Robot, switch_type: str) -> None:
57  """Initialize the Neato Connected switches."""
58  super().__init__(robot)
59  self.typetype = switch_type
60  self._state_state: dict[str, Any] | None = None
61  self._schedule_state_schedule_state: str | None = None
62  self._clean_state_clean_state = None
63  self._attr_unique_id_attr_unique_id = self.robotrobot.serial
64 
65  def update(self) -> None:
66  """Update the states of Neato switches."""
67  _LOGGER.debug("Running Neato switch update for '%s'", self.entity_identity_id)
68  try:
69  self._state_state = self.robotrobot.state
70  except NeatoRobotException as ex:
71  if self._attr_available_attr_available_attr_available: # Print only once when available
72  _LOGGER.error(
73  "Neato switch connection error for '%s': %s", self.entity_identity_id, ex
74  )
75  self._state_state = None
76  self._attr_available_attr_available_attr_available = False
77  return
78 
79  self._attr_available_attr_available_attr_available = True
80  _LOGGER.debug("self._state=%s", self._state_state)
81  if self.typetype == SWITCH_TYPE_SCHEDULE:
82  _LOGGER.debug("State: %s", self._state_state)
83  if self._state_state is not None and self._state_state["details"]["isScheduleEnabled"]:
84  self._schedule_state_schedule_state = STATE_ON
85  else:
86  self._schedule_state_schedule_state = STATE_OFF
87  _LOGGER.debug(
88  "Schedule state for '%s': %s", self.entity_identity_id, self._schedule_state_schedule_state
89  )
90 
91  @property
92  def is_on(self) -> bool:
93  """Return true if switch is on."""
94  return bool(
95  self.typetype == SWITCH_TYPE_SCHEDULE and self._schedule_state_schedule_state == STATE_ON
96  )
97 
98  def turn_on(self, **kwargs: Any) -> None:
99  """Turn the switch on."""
100  if self.typetype == SWITCH_TYPE_SCHEDULE:
101  try:
102  self.robotrobot.enable_schedule()
103  except NeatoRobotException as ex:
104  _LOGGER.error(
105  "Neato switch connection error '%s': %s", self.entity_identity_id, ex
106  )
107 
108  def turn_off(self, **kwargs: Any) -> None:
109  """Turn the switch off."""
110  if self.typetype == SWITCH_TYPE_SCHEDULE:
111  try:
112  self.robotrobot.disable_schedule()
113  except NeatoRobotException as ex:
114  _LOGGER.error(
115  "Neato switch connection error '%s': %s", self.entity_identity_id, ex
116  )
None __init__(self, NeatoHub neato, Robot robot, str switch_type)
Definition: switch.py:56
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:33