Home Assistant Unofficial Reference 2024.12.1
image.py
Go to the documentation of this file.
1 """Image platform for UniFi Network integration.
2 
3 Support for QR code for guest WLANs.
4 """
5 
6 from __future__ import annotations
7 
8 from collections.abc import Callable
9 from dataclasses import dataclass
10 
11 from aiounifi.interfaces.api_handlers import ItemEvent
12 from aiounifi.interfaces.wlans import Wlans
13 from aiounifi.models.api import ApiItemT
14 from aiounifi.models.wlan import Wlan
15 
16 from homeassistant.components.image import ImageEntity, ImageEntityDescription
17 from homeassistant.const import EntityCategory
18 from homeassistant.core import HomeAssistant, callback
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 import homeassistant.util.dt as dt_util
21 
22 from . import UnifiConfigEntry
23 from .entity import (
24  HandlerT,
25  UnifiEntity,
26  UnifiEntityDescription,
27  async_wlan_available_fn,
28  async_wlan_device_info_fn,
29 )
30 from .hub import UnifiHub
31 
32 
33 @callback
34 def async_wlan_qr_code_image_fn(hub: UnifiHub, wlan: Wlan) -> bytes:
35  """Calculate receiving data transfer value."""
36  return hub.api.wlans.generate_wlan_qr_code(wlan)
37 
38 
39 @dataclass(frozen=True, kw_only=True)
41  ImageEntityDescription, UnifiEntityDescription[HandlerT, ApiItemT]
42 ):
43  """Class describing UniFi image entity."""
44 
45  image_fn: Callable[[UnifiHub, ApiItemT], bytes]
46  value_fn: Callable[[ApiItemT], str | None]
47 
48 
49 ENTITY_DESCRIPTIONS: tuple[UnifiImageEntityDescription, ...] = (
50  UnifiImageEntityDescription[Wlans, Wlan](
51  key="WLAN QR Code",
52  translation_key="wlan_qr_code",
53  entity_category=EntityCategory.DIAGNOSTIC,
54  entity_registry_enabled_default=False,
55  api_handler_fn=lambda api: api.wlans,
56  available_fn=async_wlan_available_fn,
57  device_info_fn=async_wlan_device_info_fn,
58  name_fn=lambda wlan: "QR Code",
59  object_fn=lambda api, obj_id: api.wlans[obj_id],
60  unique_id_fn=lambda hub, obj_id: f"qr_code-{obj_id}",
61  image_fn=async_wlan_qr_code_image_fn,
62  value_fn=lambda obj: obj.x_passphrase,
63  ),
64 )
65 
66 
68  hass: HomeAssistant,
69  config_entry: UnifiConfigEntry,
70  async_add_entities: AddEntitiesCallback,
71 ) -> None:
72  """Set up image platform for UniFi Network integration."""
73  config_entry.runtime_data.entity_loader.register_platform(
74  async_add_entities, UnifiImageEntity, ENTITY_DESCRIPTIONS, requires_admin=True
75  )
76 
77 
78 class UnifiImageEntity(UnifiEntity[HandlerT, ApiItemT], ImageEntity):
79  """Base representation of a UniFi image."""
80 
81  entity_description: UnifiImageEntityDescription[HandlerT, ApiItemT]
82  _attr_content_type = "image/png"
83 
84  current_image: bytes | None = None
85  previous_value: str | None = None
86 
87  def __init__(
88  self,
89  obj_id: str,
90  hub: UnifiHub,
91  description: UnifiEntityDescription[HandlerT, ApiItemT],
92  ) -> None:
93  """Initiatlize UniFi Image entity."""
94  super().__init__(obj_id, hub, description)
95  ImageEntity.__init__(self, hub.hass)
96 
97  def image(self) -> bytes | None:
98  """Return bytes of image."""
99  if self.current_imagecurrent_image is None:
100  description = self.entity_descriptionentity_description
101  obj = description.object_fn(self.apiapi, self._obj_id_obj_id)
102  self.current_imagecurrent_image = description.image_fn(self.hubhub, obj)
103  return self.current_imagecurrent_image
104 
105  @callback
106  def async_update_state(self, event: ItemEvent, obj_id: str) -> None:
107  """Update entity state."""
108  description = self.entity_descriptionentity_description
109  obj = description.object_fn(self.apiapi, self._obj_id_obj_id)
110  if (value := description.value_fn(obj)) != self.previous_valueprevious_value:
111  self.previous_valueprevious_value = value
112  self.current_imagecurrent_image = None
113  self._attr_image_last_updated_attr_image_last_updated = dt_util.utcnow()
None async_update_state(self, ItemEvent event, str obj_id)
Definition: image.py:106
None __init__(self, str obj_id, UnifiHub hub, UnifiEntityDescription[HandlerT, ApiItemT] description)
Definition: image.py:92
bytes async_wlan_qr_code_image_fn(UnifiHub hub, Wlan wlan)
Definition: image.py:34
None async_setup_entry(HomeAssistant hass, UnifiConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: image.py:71