Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Genius Hub switch/outlet devices."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 from typing import Any
7 
8 import voluptuous as vol
9 
10 from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import config_validation as cv, entity_platform
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 from homeassistant.helpers.typing import VolDictType
15 
16 from . import ATTR_DURATION, GeniusHubConfigEntry
17 from .entity import GeniusZone
18 
19 GH_ON_OFF_ZONE = "on / off"
20 
21 SVC_SET_SWITCH_OVERRIDE = "set_switch_override"
22 
23 SET_SWITCH_OVERRIDE_SCHEMA: VolDictType = {
24  vol.Optional(ATTR_DURATION): vol.All(
25  cv.time_period,
26  vol.Range(min=timedelta(minutes=5), max=timedelta(days=1)),
27  ),
28 }
29 
30 
32  hass: HomeAssistant,
33  entry: GeniusHubConfigEntry,
34  async_add_entities: AddEntitiesCallback,
35 ) -> None:
36  """Set up the Genius Hub switch entities."""
37 
38  broker = entry.runtime_data
39 
41  GeniusSwitch(broker, z)
42  for z in broker.client.zone_objs
43  if z.data.get("type") == GH_ON_OFF_ZONE
44  )
45 
46  # Register custom services
47  platform = entity_platform.async_get_current_platform()
48 
49  platform.async_register_entity_service(
50  SVC_SET_SWITCH_OVERRIDE,
51  SET_SWITCH_OVERRIDE_SCHEMA,
52  "async_turn_on",
53  )
54 
55 
57  """Representation of a Genius Hub switch."""
58 
59  @property
60  def device_class(self):
61  """Return the class of this device, from component DEVICE_CLASSES."""
62  return SwitchDeviceClass.OUTLET
63 
64  @property
65  def is_on(self) -> bool:
66  """Return the current state of the on/off zone.
67 
68  The zone is considered 'on' if the mode is either 'override' or 'timer'.
69  """
70  return (
71  self._zone_zone.data["mode"] in ["override", "timer"]
72  and self._zone_zone.data["setpoint"]
73  )
74 
75  async def async_turn_off(self, **kwargs: Any) -> None:
76  """Send the zone to Timer mode.
77 
78  The zone is deemed 'off' in this mode, although the plugs may actually be on.
79  """
80  await self._zone_zone.set_mode("timer")
81 
82  async def async_turn_on(self, **kwargs: Any) -> None:
83  """Set the zone to override/on ({'setpoint': true}) for x seconds."""
84  await self._zone_zone.set_override(1, kwargs.get(ATTR_DURATION, 3600))
None async_setup_entry(HomeAssistant hass, GeniusHubConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:35