Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Magic Home switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from flux_led import DeviceType
8 from flux_led.aio import AIOWifiLedBulb
9 from flux_led.const import MODE_MUSIC
10 
11 from homeassistant import config_entries
12 from homeassistant.components.switch import SwitchEntity
13 from homeassistant.const import EntityCategory
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 from homeassistant.helpers.update_coordinator import CoordinatorEntity
17 
18 from .const import (
19  CONF_REMOTE_ACCESS_ENABLED,
20  CONF_REMOTE_ACCESS_HOST,
21  CONF_REMOTE_ACCESS_PORT,
22  DOMAIN,
23 )
24 from .coordinator import FluxLedUpdateCoordinator
25 from .discovery import async_clear_discovery_cache
26 from .entity import FluxBaseEntity, FluxEntity, FluxOnOffEntity
27 
28 
30  hass: HomeAssistant,
32  async_add_entities: AddEntitiesCallback,
33 ) -> None:
34  """Set up the Flux lights."""
35  coordinator: FluxLedUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
36  entities: list[FluxSwitch | FluxRemoteAccessSwitch | FluxMusicSwitch] = []
37  base_unique_id = entry.unique_id or entry.entry_id
38 
39  if coordinator.device.device_type == DeviceType.Switch:
40  entities.append(FluxSwitch(coordinator, base_unique_id, None))
41 
42  if entry.data.get(CONF_REMOTE_ACCESS_HOST):
43  entities.append(FluxRemoteAccessSwitch(coordinator.device, entry))
44 
45  if coordinator.device.microphone:
46  entities.append(FluxMusicSwitch(coordinator, base_unique_id, "music"))
47 
48  async_add_entities(entities)
49 
50 
51 class FluxSwitch(
52  FluxOnOffEntity, CoordinatorEntity[FluxLedUpdateCoordinator], SwitchEntity
53 ):
54  """Representation of a Flux switch."""
55 
56  _attr_name = None
57 
58  async def _async_turn_on(self, **kwargs: Any) -> None:
59  """Turn the device on."""
60  if not self.is_onis_onis_on:
61  await self._device.async_turn_on()
62 
63 
65  """Representation of a Flux remote access switch."""
66 
67  _attr_entity_category = EntityCategory.CONFIG
68  _attr_translation_key = "remote_access"
69 
70  def __init__(
71  self,
72  device: AIOWifiLedBulb,
74  ) -> None:
75  """Initialize the light."""
76  super().__init__(device, entry)
77  base_unique_id = entry.unique_id or entry.entry_id
78  self._attr_unique_id_attr_unique_id = f"{base_unique_id}_remote_access"
79 
80  async def async_turn_on(self, **kwargs: Any) -> None:
81  """Turn the remote access on."""
82  await self._device.async_enable_remote_access(
83  self.entryentry.data[CONF_REMOTE_ACCESS_HOST],
84  self.entryentry.data[CONF_REMOTE_ACCESS_PORT],
85  )
86  await self._async_update_entry_async_update_entry(True)
87 
88  async def _async_update_entry(self, new_state: bool) -> None:
89  """Update the entry with the new state on success."""
90  async_clear_discovery_cache(self.hasshass, self._device.ipaddr)
91  self.hasshass.config_entries.async_update_entry(
92  self.entryentry,
93  data={**self.entryentry.data, CONF_REMOTE_ACCESS_ENABLED: new_state},
94  )
95  self.async_write_ha_stateasync_write_ha_state()
96 
97  async def async_turn_off(self, **kwargs: Any) -> None:
98  """Turn the remote access off."""
99  await self._device.async_disable_remote_access()
100  await self._async_update_entry_async_update_entry(False)
101 
102  @property
103  def is_on(self) -> bool:
104  """Return true if remote access is enabled."""
105  return bool(self.entryentry.data[CONF_REMOTE_ACCESS_ENABLED])
106 
107 
109  """Representation of a Flux music switch."""
110 
111  _attr_translation_key = "music"
112 
113  async def async_turn_on(self, **kwargs: Any) -> None:
114  """Turn the microphone on."""
115  await self._async_ensure_device_on_async_ensure_device_on()
116  await self._device.async_set_music_mode()
117  self.async_write_ha_stateasync_write_ha_state()
118  await self.coordinator.async_request_refresh()
119 
120  async def async_turn_off(self, **kwargs: Any) -> None:
121  """Turn the microphone off."""
122  await self._device.async_set_levels(*self._device.rgb, brightness=255)
123  self.async_write_ha_stateasync_write_ha_state()
124  await self.coordinator.async_request_refresh()
125 
126  @property
127  def is_on(self) -> bool:
128  """Return true if microphone is is on."""
129  return self._device.is_on and self._device.effect == MODE_MUSIC
None __init__(self, AIOWifiLedBulb device, config_entries.ConfigEntry entry)
Definition: switch.py:74
None async_clear_discovery_cache(HomeAssistant hass, str host)
Definition: discovery.py:164
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:33