Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for switch platform for Hue resources (V2 only)."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiohue.v2 import HueBridgeV2
8 from aiohue.v2.controllers.config import BehaviorInstance, BehaviorInstanceController
9 from aiohue.v2.controllers.events import EventType
10 from aiohue.v2.controllers.sensors import (
11  LightLevel,
12  LightLevelController,
13  Motion,
14  MotionController,
15 )
16 
18  SwitchDeviceClass,
19  SwitchEntity,
20  SwitchEntityDescription,
21 )
22 from homeassistant.config_entries import ConfigEntry
23 from homeassistant.const import EntityCategory
24 from homeassistant.core import HomeAssistant, callback
25 from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 
27 from .bridge import HueBridge
28 from .const import DOMAIN
29 from .v2.entity import HueBaseEntity
30 
31 
33  hass: HomeAssistant,
34  config_entry: ConfigEntry,
35  async_add_entities: AddEntitiesCallback,
36 ) -> None:
37  """Set up Hue switch platform from Hue resources."""
38  bridge: HueBridge = hass.data[DOMAIN][config_entry.entry_id]
39  api: HueBridgeV2 = bridge.api
40 
41  if bridge.api_version == 1:
42  # should not happen, but just in case
43  raise NotImplementedError("Switch support is only available for V2 bridges")
44 
45  @callback
46  def register_items(
47  controller: BehaviorInstanceController
48  | LightLevelController
49  | MotionController,
50  switch_class: type[
51  HueBehaviorInstanceEnabledEntity
52  | HueLightSensorEnabledEntity
53  | HueMotionSensorEnabledEntity
54  ],
55  ):
56  @callback
57  def async_add_entity(
58  event_type: EventType, resource: BehaviorInstance | LightLevel | Motion
59  ) -> None:
60  """Add entity from Hue resource."""
61  async_add_entities([switch_class(bridge, controller, resource)])
62 
63  # add all current items in controller
64  for item in controller:
65  async_add_entity(EventType.RESOURCE_ADDED, item)
66 
67  # register listener for new items only
68  config_entry.async_on_unload(
69  controller.subscribe(
70  async_add_entity, event_filter=EventType.RESOURCE_ADDED
71  )
72  )
73 
74  # setup for each switch-type hue resource
75  register_items(api.sensors.motion, HueMotionSensorEnabledEntity)
76  register_items(api.sensors.light_level, HueLightSensorEnabledEntity)
77  register_items(api.config.behavior_instance, HueBehaviorInstanceEnabledEntity)
78 
79 
81  """Representation of a Switch entity from a Hue resource that can be toggled enabled."""
82 
83  controller: BehaviorInstanceController | LightLevelController | MotionController
84  resource: BehaviorInstance | LightLevel | Motion
85 
86  entity_description = SwitchEntityDescription(
87  key="sensing_service_enabled",
88  device_class=SwitchDeviceClass.SWITCH,
89  entity_category=EntityCategory.CONFIG,
90  has_entity_name=True,
91  )
92 
93  @property
94  def is_on(self) -> bool:
95  """Return true if the switch is on."""
96  return self.resourceresource.enabled
97 
98  async def async_turn_on(self, **kwargs: Any) -> None:
99  """Turn the entity on."""
100  await self.bridgebridge.async_request_call(
101  self.controllercontroller.set_enabled, self.resourceresource.id, enabled=True
102  )
103 
104  async def async_turn_off(self, **kwargs: Any) -> None:
105  """Turn the entity on."""
106  await self.bridgebridge.async_request_call(
107  self.controllercontroller.set_enabled, self.resourceresource.id, enabled=False
108  )
109 
110 
112  """Representation of a Switch entity to enable/disable a Hue Behavior Instance."""
113 
114  resource: BehaviorInstance
115 
116  entity_description = SwitchEntityDescription(
117  key="behavior_instance",
118  device_class=SwitchDeviceClass.SWITCH,
119  entity_category=EntityCategory.CONFIG,
120  has_entity_name=False,
121  )
122 
123  @property
124  def name(self) -> str:
125  """Return name for this entity."""
126  return f"Automation: {self.resource.metadata.name}"
127 
128 
130  """Representation of a Switch entity to enable/disable a Hue motion sensor."""
131 
132  entity_description = SwitchEntityDescription(
133  key="motion_sensor_enabled",
134  device_class=SwitchDeviceClass.SWITCH,
135  entity_category=EntityCategory.CONFIG,
136  has_entity_name=True,
137  translation_key="motion_sensor_enabled",
138  )
139 
140 
142  """Representation of a Switch entity to enable/disable a Hue light sensor."""
143 
144  entity_description = SwitchEntityDescription(
145  key="light_sensor_enabled",
146  device_class=SwitchDeviceClass.SWITCH,
147  entity_category=EntityCategory.CONFIG,
148  has_entity_name=True,
149  translation_key="light_sensor_enabled",
150  )
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:36