Home Assistant Unofficial Reference 2024.12.1
text.py
Go to the documentation of this file.
1 """Text for Shelly."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import TYPE_CHECKING, Final
7 
8 from aioshelly.const import RPC_GENERATIONS
9 
11  DOMAIN as TEXT_PLATFORM,
12  TextEntity,
13  TextEntityDescription,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .coordinator import ShellyConfigEntry
19 from .entity import (
20  RpcEntityDescription,
21  ShellyRpcAttributeEntity,
22  async_setup_entry_rpc,
23 )
24 from .utils import (
25  async_remove_orphaned_entities,
26  get_device_entry_gen,
27  get_virtual_component_ids,
28 )
29 
30 
31 @dataclass(frozen=True, kw_only=True)
33  """Class to describe a RPC text entity."""
34 
35 
36 RPC_TEXT_ENTITIES: Final = {
37  "text": RpcTextDescription(
38  key="text",
39  sub_key="value",
40  has_entity_name=True,
41  ),
42 }
43 
44 
46  hass: HomeAssistant,
47  config_entry: ShellyConfigEntry,
48  async_add_entities: AddEntitiesCallback,
49 ) -> None:
50  """Set up sensors for device."""
51  if get_device_entry_gen(config_entry) in RPC_GENERATIONS:
52  coordinator = config_entry.runtime_data.rpc
53  assert coordinator
54 
56  hass, config_entry, async_add_entities, RPC_TEXT_ENTITIES, RpcText
57  )
58 
59  # the user can remove virtual components from the device configuration, so
60  # we need to remove orphaned entities
61  virtual_text_ids = get_virtual_component_ids(
62  coordinator.device.config, TEXT_PLATFORM
63  )
65  hass,
66  config_entry.entry_id,
67  coordinator.mac,
68  TEXT_PLATFORM,
69  virtual_text_ids,
70  "text",
71  )
72 
73 
75  """Represent a RPC text entity."""
76 
77  entity_description: RpcTextDescription
78 
79  @property
80  def native_value(self) -> str | None:
81  """Return value of sensor."""
82  if TYPE_CHECKING:
83  assert isinstance(self.attribute_valueattribute_value, str | None)
84 
85  return self.attribute_valueattribute_value
86 
87  async def async_set_value(self, value: str) -> None:
88  """Change the value."""
89  await self.call_rpccall_rpc("Text.Set", {"id": self._id_id, "value": value})
Any call_rpc(self, str method, Any params)
Definition: entity.py:384
None async_set_value(self, str value)
Definition: text.py:87
None async_setup_entry_rpc(HomeAssistant hass, ShellyConfigEntry config_entry, AddEntitiesCallback async_add_entities, Mapping[str, RpcEntityDescription] sensors, Callable sensor_class)
Definition: entity.py:142
None async_setup_entry(HomeAssistant hass, ShellyConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: text.py:49
list[str] get_virtual_component_ids(dict[str, Any] config, str platform)
Definition: utils.py:528
None async_remove_orphaned_entities(HomeAssistant hass, str config_entry_id, str mac, str platform, Iterable[str] keys, str|None key_suffix=None)
Definition: utils.py:555
int get_device_entry_gen(ConfigEntry entry)
Definition: utils.py:353