1 """Image platform for UniFi Network integration.
3 Support for QR code for guest WLANs.
6 from __future__
import annotations
8 from collections.abc
import Callable
9 from dataclasses
import dataclass
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
22 from .
import UnifiConfigEntry
26 UnifiEntityDescription,
27 async_wlan_available_fn,
28 async_wlan_device_info_fn,
30 from .hub
import UnifiHub
35 """Calculate receiving data transfer value."""
36 return hub.api.wlans.generate_wlan_qr_code(wlan)
39 @dataclass(frozen=True, kw_only=True)
41 ImageEntityDescription, UnifiEntityDescription[HandlerT, ApiItemT]
43 """Class describing UniFi image entity."""
45 image_fn: Callable[[UnifiHub, ApiItemT], bytes]
46 value_fn: Callable[[ApiItemT], str |
None]
49 ENTITY_DESCRIPTIONS: tuple[UnifiImageEntityDescription, ...] = (
50 UnifiImageEntityDescription[Wlans, Wlan](
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,
69 config_entry: UnifiConfigEntry,
70 async_add_entities: AddEntitiesCallback,
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
79 """Base representation of a UniFi image."""
81 entity_description: UnifiImageEntityDescription[HandlerT, ApiItemT]
82 _attr_content_type =
"image/png"
84 current_image: bytes |
None =
None
85 previous_value: str |
None =
None
91 description: UnifiEntityDescription[HandlerT, ApiItemT],
93 """Initiatlize UniFi Image entity."""
94 super().
__init__(obj_id, hub, description)
95 ImageEntity.__init__(self, hub.hass)
97 def image(self) -> bytes | None:
98 """Return bytes of image."""
101 obj = description.object_fn(self.
apiapi, self.
_obj_id_obj_id)
107 """Update entity state."""
109 obj = description.object_fn(self.
apiapi, self.
_obj_id_obj_id)
110 if (value := description.value_fn(obj)) != self.
previous_valueprevious_value:
None async_update_state(self, ItemEvent event, str obj_id)
None __init__(self, str obj_id, UnifiHub hub, UnifiEntityDescription[HandlerT, ApiItemT] description)
bytes async_wlan_qr_code_image_fn(UnifiHub hub, Wlan wlan)
None async_setup_entry(HomeAssistant hass, UnifiConfigEntry config_entry, AddEntitiesCallback async_add_entities)