Home Assistant Unofficial Reference 2024.12.1
text.py
Go to the documentation of this file.
1 """Text entities for UniFi Protect."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Sequence
6 from dataclasses import dataclass
7 
8 from uiprotect.data import (
9  Camera,
10  DoorbellMessageType,
11  ModelType,
12  ProtectAdoptableDeviceModel,
13 )
14 
15 from homeassistant.components.text import TextEntity, TextEntityDescription
16 from homeassistant.const import EntityCategory
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .data import ProtectDeviceType, UFPConfigEntry
21 from .entity import (
22  PermRequired,
23  ProtectDeviceEntity,
24  ProtectEntityDescription,
25  ProtectSetableKeysMixin,
26  T,
27  async_all_device_entities,
28 )
29 
30 
31 @dataclass(frozen=True, kw_only=True)
32 class ProtectTextEntityDescription(ProtectSetableKeysMixin[T], TextEntityDescription):
33  """Describes UniFi Protect Text entity."""
34 
35 
36 def _get_doorbell_current(obj: Camera) -> str | None:
37  if obj.lcd_message is None:
38  return obj.api.bootstrap.nvr.doorbell_settings.default_message_text
39  return obj.lcd_message.text
40 
41 
42 async def _set_doorbell_message(obj: Camera, message: str) -> None:
43  await obj.set_lcd_text(DoorbellMessageType.CUSTOM_MESSAGE, text=message)
44 
45 
46 CAMERA: tuple[ProtectTextEntityDescription, ...] = (
48  key="doorbell",
49  name="Doorbell",
50  entity_category=EntityCategory.CONFIG,
51  ufp_value_fn=_get_doorbell_current,
52  ufp_set_method_fn=_set_doorbell_message,
53  ufp_required_field="feature_flags.has_lcd_screen",
54  ufp_perm=PermRequired.WRITE,
55  ),
56 )
57 
58 _MODEL_DESCRIPTIONS: dict[ModelType, Sequence[ProtectEntityDescription]] = {
59  ModelType.CAMERA: CAMERA,
60 }
61 
62 
64  hass: HomeAssistant,
65  entry: UFPConfigEntry,
66  async_add_entities: AddEntitiesCallback,
67 ) -> None:
68  """Set up sensors for UniFi Protect integration."""
69  data = entry.runtime_data
70 
71  @callback
72  def _add_new_device(device: ProtectAdoptableDeviceModel) -> None:
75  data,
76  ProtectDeviceText,
77  model_descriptions=_MODEL_DESCRIPTIONS,
78  ufp_device=device,
79  )
80  )
81 
82  data.async_subscribe_adopt(_add_new_device)
85  data, ProtectDeviceText, model_descriptions=_MODEL_DESCRIPTIONS
86  )
87  )
88 
89 
91  """A Ubiquiti UniFi Protect Sensor."""
92 
93  entity_description: ProtectTextEntityDescription
94  _state_attrs = ("_attr_available", "_attr_native_value")
95 
96  @callback
97  def _async_update_device_from_protect(self, device: ProtectDeviceType) -> None:
98  super()._async_update_device_from_protect(device)
99  self._attr_native_value_attr_native_value = self.entity_descriptionentity_description.get_ufp_value(self.devicedevice)
100 
101  async def async_set_value(self, value: str) -> None:
102  """Change the value."""
103  await self.entity_descriptionentity_description.ufp_set(self.devicedevice, value)
None _async_update_device_from_protect(self, ProtectDeviceType device)
Definition: text.py:97
list[BaseProtectEntity] async_all_device_entities(ProtectData data, type[BaseProtectEntity] klass, dict[ModelType, Sequence[ProtectEntityDescription]]|None model_descriptions=None, Sequence[ProtectEntityDescription]|None all_descs=None, list[ProtectEntityDescription]|None unadopted_descs=None, ProtectAdoptableDeviceModel|None ufp_device=None)
Definition: entity.py:153
str|None _get_doorbell_current(Camera obj)
Definition: text.py:36
None async_setup_entry(HomeAssistant hass, UFPConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: text.py:67
None _set_doorbell_message(Camera obj, str message)
Definition: text.py:42