Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Cambridge Audio switch entities."""
2 
3 from collections.abc import Awaitable, Callable
4 from dataclasses import dataclass
5 from typing import Any
6 
7 from aiostreammagic import StreamMagicClient
8 
9 from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import EntityCategory
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .entity import CambridgeAudioEntity, command
16 
17 PARALLEL_UPDATES = 0
18 
19 
20 @dataclass(frozen=True, kw_only=True)
22  """Describes Cambridge Audio switch entity."""
23 
24  value_fn: Callable[[StreamMagicClient], bool]
25  set_value_fn: Callable[[StreamMagicClient, bool], Awaitable[None]]
26 
27 
28 CONTROL_ENTITIES: tuple[CambridgeAudioSwitchEntityDescription, ...] = (
30  key="pre_amp",
31  translation_key="pre_amp",
32  entity_category=EntityCategory.CONFIG,
33  value_fn=lambda client: client.state.pre_amp_mode,
34  set_value_fn=lambda client, value: client.set_pre_amp_mode(value),
35  ),
37  key="early_update",
38  translation_key="early_update",
39  entity_category=EntityCategory.CONFIG,
40  value_fn=lambda client: client.update.early_update,
41  set_value_fn=lambda client, value: client.set_early_update(value),
42  ),
43 )
44 
45 
47  hass: HomeAssistant,
48  entry: ConfigEntry,
49  async_add_entities: AddEntitiesCallback,
50 ) -> None:
51  """Set up Cambridge Audio switch entities based on a config entry."""
53  CambridgeAudioSwitch(entry.runtime_data, description)
54  for description in CONTROL_ENTITIES
55  )
56 
57 
59  """Defines a Cambridge Audio switch entity."""
60 
61  entity_description: CambridgeAudioSwitchEntityDescription
62 
63  def __init__(
64  self,
65  client: StreamMagicClient,
66  description: CambridgeAudioSwitchEntityDescription,
67  ) -> None:
68  """Initialize Cambridge Audio switch."""
69  super().__init__(client)
70  self.entity_descriptionentity_description = description
71  self._attr_unique_id_attr_unique_id = f"{client.info.unit_id}-{description.key}"
72 
73  @property
74  def is_on(self) -> bool:
75  """Return the state of the switch."""
76  return self.entity_descriptionentity_description.value_fn(self.clientclient)
77 
78  @command
79  async def async_turn_on(self, **kwargs: Any) -> None:
80  """Turn the switch on."""
81  await self.entity_descriptionentity_description.set_value_fn(self.clientclient, True)
82 
83  @command
84  async def async_turn_off(self, **kwargs: Any) -> None:
85  """Turn the switch off."""
86  await self.entity_descriptionentity_description.set_value_fn(self.clientclient, False)
None __init__(self, StreamMagicClient client, CambridgeAudioSwitchEntityDescription description)
Definition: switch.py:67
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:50