Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base entity for the Fully Kiosk Browser integration."""
2 
3 from __future__ import annotations
4 
5 import json
6 
7 from yarl import URL
8 
9 from homeassistant.components import mqtt
10 from homeassistant.const import ATTR_CONNECTIONS
11 from homeassistant.core import CALLBACK_TYPE, callback
12 from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
13 from homeassistant.helpers.entity import Entity
14 from homeassistant.helpers.update_coordinator import CoordinatorEntity
15 
16 from .const import DOMAIN
17 from .coordinator import FullyKioskDataUpdateCoordinator
18 
19 
20 def valid_global_mac_address(mac: str | None) -> bool:
21  """Check if a MAC address is valid, non-locally administered address."""
22  if not isinstance(mac, str):
23  return False
24  try:
25  first_octet = int(mac.split(":")[0], 16)
26  # If the second least-significant bit is set, it's a locally administered address, should not be used as an ID
27  return not bool(first_octet & 0x2)
28  except ValueError:
29  return False
30 
31 
32 class FullyKioskEntity(CoordinatorEntity[FullyKioskDataUpdateCoordinator], Entity):
33  """Defines a Fully Kiosk Browser entity."""
34 
35  _attr_has_entity_name = True
36 
37  def __init__(self, coordinator: FullyKioskDataUpdateCoordinator) -> None:
38  """Initialize the Fully Kiosk Browser entity."""
39  super().__init__(coordinator=coordinator)
40 
41  url = URL.build(
42  scheme="https" if coordinator.use_ssl else "http",
43  host=coordinator.data["ip4"],
44  port=2323,
45  )
46 
47  device_info = DeviceInfo(
48  identifiers={(DOMAIN, coordinator.data["deviceID"])},
49  name=coordinator.data["deviceName"],
50  manufacturer=coordinator.data["deviceManufacturer"],
51  model=coordinator.data["deviceModel"],
52  sw_version=coordinator.data["appVersionName"],
53  configuration_url=str(url),
54  )
55  if "Mac" in coordinator.data and valid_global_mac_address(
56  coordinator.data["Mac"]
57  ):
58  device_info[ATTR_CONNECTIONS] = {
59  (CONNECTION_NETWORK_MAC, coordinator.data["Mac"])
60  }
61  self._attr_device_info_attr_device_info = device_info
62 
63  async def mqtt_subscribe(
64  self, event: str | None, event_callback: CALLBACK_TYPE
65  ) -> CALLBACK_TYPE | None:
66  """Subscribe to MQTT for a given event."""
67  data = self.coordinator.data
68  if (
69  event is None
70  or not mqtt.mqtt_config_entry_enabled(self.hasshasshass)
71  or not data["settings"]["mqttEnabled"]
72  ):
73  return None
74 
75  @callback
76  def message_callback(message: mqtt.ReceiveMessage) -> None:
77  payload = json.loads(message.payload)
78  if "event" in payload and payload["event"] == event:
79  event_callback(**payload)
80 
81  topic_template = data["settings"]["mqttEventTopic"]
82  topic = (
83  topic_template.replace("$appId", "fully")
84  .replace("$event", event)
85  .replace("$deviceId", data["deviceID"])
86  )
87 
88  return await mqtt.async_subscribe(self.hasshasshass, topic, message_callback)
None __init__(self, FullyKioskDataUpdateCoordinator coordinator)
Definition: entity.py:37
CALLBACK_TYPE|None mqtt_subscribe(self, str|None event, CALLBACK_TYPE event_callback)
Definition: entity.py:65
bool valid_global_mac_address(str|None mac)
Definition: entity.py:20