Home Assistant Unofficial Reference 2024.12.1
image.py
Go to the documentation of this file.
1 """FRITZ image integration."""
2 
3 from __future__ import annotations
4 
5 from io import BytesIO
6 import logging
7 
8 from requests.exceptions import RequestException
9 
10 from homeassistant.components.image import ImageEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import EntityCategory
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 from homeassistant.util import dt as dt_util, slugify
16 
17 from .const import DOMAIN
18 from .coordinator import AvmWrapper
19 from .entity import FritzBoxBaseEntity
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
25  hass: HomeAssistant,
26  entry: ConfigEntry,
27  async_add_entities: AddEntitiesCallback,
28 ) -> None:
29  """Set up guest WiFi QR code for device."""
30  avm_wrapper: AvmWrapper = hass.data[DOMAIN][entry.entry_id]
31 
32  guest_wifi_info = await hass.async_add_executor_job(
33  avm_wrapper.fritz_guest_wifi.get_info
34  )
35 
37  [
39  hass, avm_wrapper, entry.title, guest_wifi_info["NewSSID"]
40  )
41  ]
42  )
43 
44 
46  """Implementation of the FritzBox guest wifi QR code image entity."""
47 
48  _attr_content_type = "image/png"
49  _attr_entity_category = EntityCategory.DIAGNOSTIC
50  _attr_has_entity_name = True
51  _attr_should_poll = True
52 
53  def __init__(
54  self,
55  hass: HomeAssistant,
56  avm_wrapper: AvmWrapper,
57  device_friendly_name: str,
58  ssid: str,
59  ) -> None:
60  """Initialize the image entity."""
61  self._attr_name_attr_name = ssid
62  self._attr_unique_id_attr_unique_id = slugify(f"{avm_wrapper.unique_id}-{ssid}-qr-code")
63  self._current_qr_bytes_current_qr_bytes: bytes | None = None
64  super().__init__(avm_wrapper, device_friendly_name)
65  ImageEntity.__init__(self, hass)
66 
67  async def _fetch_image(self) -> bytes:
68  """Fetch the QR code from the Fritz!Box."""
69  qr_stream: BytesIO = await self.hasshass.async_add_executor_job(
70  self._avm_wrapper_avm_wrapper.fritz_guest_wifi.get_wifi_qr_code, "png"
71  )
72  qr_bytes = qr_stream.getvalue()
73  _LOGGER.debug("fetched %s bytes", len(qr_bytes))
74 
75  return qr_bytes
76 
77  async def async_added_to_hass(self) -> None:
78  """Fetch and set initial data and state."""
79  self._current_qr_bytes_current_qr_bytes = await self._fetch_image_fetch_image()
80  self._attr_image_last_updated_attr_image_last_updated = dt_util.utcnow()
81 
82  async def async_update(self) -> None:
83  """Update the image entity data."""
84  try:
85  qr_bytes = await self._fetch_image_fetch_image()
86  except RequestException:
87  self._current_qr_bytes_current_qr_bytes = None
88  self._attr_image_last_updated_attr_image_last_updated = None
89  self.async_write_ha_stateasync_write_ha_state()
90  return
91 
92  if self._current_qr_bytes_current_qr_bytes != qr_bytes:
93  dt_now = dt_util.utcnow()
94  _LOGGER.debug("qr code has changed, reset image last updated property")
95  self._attr_image_last_updated_attr_image_last_updated = dt_now
96  self._current_qr_bytes_current_qr_bytes = qr_bytes
97  self.async_write_ha_stateasync_write_ha_state()
98 
99  async def async_image(self) -> bytes | None:
100  """Return bytes of image."""
101  return self._current_qr_bytes_current_qr_bytes
None __init__(self, HomeAssistant hass, AvmWrapper avm_wrapper, str device_friendly_name, str ssid)
Definition: image.py:59
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: image.py:28