Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Fully Kiosk Browser sensor."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfInformation
16 from homeassistant.core import HomeAssistant, callback
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.helpers.typing import StateType
19 
20 from . import FullyKioskConfigEntry
21 from .coordinator import FullyKioskDataUpdateCoordinator
22 from .entity import FullyKioskEntity
23 
24 
25 def round_storage(value: int) -> float:
26  """Convert storage values from bytes to megabytes."""
27  return round(value * 0.000001, 1)
28 
29 
30 def truncate_url(value: StateType) -> tuple[StateType, dict[str, Any]]:
31  """Truncate URL if longer than 256."""
32  url = str(value)
33  truncated = len(url) > 256
34  extra_state_attributes = {
35  "full_url": url,
36  "truncated": truncated,
37  }
38  if truncated:
39  return (url[0:255], extra_state_attributes)
40  return (url, extra_state_attributes)
41 
42 
43 @dataclass(frozen=True)
45  """Fully Kiosk Browser sensor description."""
46 
47  round_state_value: bool = False
48  state_fn: Callable[[StateType], tuple[StateType, dict[str, Any]]] | None = None
49 
50 
51 SENSORS: tuple[FullySensorEntityDescription, ...] = (
53  key="batteryLevel",
54  device_class=SensorDeviceClass.BATTERY,
55  native_unit_of_measurement=PERCENTAGE,
56  state_class=SensorStateClass.MEASUREMENT,
57  entity_category=EntityCategory.DIAGNOSTIC,
58  ),
60  key="currentPage",
61  translation_key="current_page",
62  entity_category=EntityCategory.DIAGNOSTIC,
63  state_fn=truncate_url,
64  ),
66  key="screenOrientation",
67  translation_key="screen_orientation",
68  entity_category=EntityCategory.DIAGNOSTIC,
69  ),
71  key="foregroundApp",
72  translation_key="foreground_app",
73  entity_category=EntityCategory.DIAGNOSTIC,
74  ),
76  key="internalStorageFreeSpace",
77  translation_key="internal_storage_free_space",
78  entity_category=EntityCategory.DIAGNOSTIC,
79  native_unit_of_measurement=UnitOfInformation.MEGABYTES,
80  device_class=SensorDeviceClass.DATA_SIZE,
81  state_class=SensorStateClass.MEASUREMENT,
82  round_state_value=True,
83  ),
85  key="internalStorageTotalSpace",
86  translation_key="internal_storage_total_space",
87  entity_category=EntityCategory.DIAGNOSTIC,
88  native_unit_of_measurement=UnitOfInformation.MEGABYTES,
89  device_class=SensorDeviceClass.DATA_SIZE,
90  state_class=SensorStateClass.MEASUREMENT,
91  round_state_value=True,
92  ),
94  key="ramFreeMemory",
95  translation_key="ram_free_memory",
96  entity_category=EntityCategory.DIAGNOSTIC,
97  native_unit_of_measurement=UnitOfInformation.MEGABYTES,
98  device_class=SensorDeviceClass.DATA_SIZE,
99  state_class=SensorStateClass.MEASUREMENT,
100  round_state_value=True,
101  ),
103  key="ramTotalMemory",
104  translation_key="ram_total_memory",
105  entity_category=EntityCategory.DIAGNOSTIC,
106  native_unit_of_measurement=UnitOfInformation.MEGABYTES,
107  device_class=SensorDeviceClass.DATA_SIZE,
108  state_class=SensorStateClass.MEASUREMENT,
109  round_state_value=True,
110  ),
111 )
112 
113 
115  hass: HomeAssistant,
116  config_entry: FullyKioskConfigEntry,
117  async_add_entities: AddEntitiesCallback,
118 ) -> None:
119  """Set up the Fully Kiosk Browser sensor."""
120  coordinator = config_entry.runtime_data
122  FullySensor(coordinator, description)
123  for description in SENSORS
124  if description.key in coordinator.data
125  )
126 
127 
129  """Representation of a Fully Kiosk Browser sensor."""
130 
131  entity_description: FullySensorEntityDescription
132 
133  def __init__(
134  self,
135  coordinator: FullyKioskDataUpdateCoordinator,
136  sensor: FullySensorEntityDescription,
137  ) -> None:
138  """Initialize the sensor entity."""
139  self.entity_descriptionentity_description = sensor
140 
141  self._attr_unique_id_attr_unique_id = f"{coordinator.data['deviceID']}-{sensor.key}"
142 
143  super().__init__(coordinator)
144 
145  @callback
146  def _handle_coordinator_update(self) -> None:
147  extra_state_attributes: dict[str, Any] = {}
148  value = self.coordinator.data.get(self.entity_descriptionentity_description.key)
149 
150  if value is not None:
151  if self.entity_descriptionentity_description.state_fn is not None:
152  value, extra_state_attributes = self.entity_descriptionentity_description.state_fn(value)
153 
154  if self.entity_descriptionentity_description.round_state_value:
155  value = round_storage(value)
156 
157  self._attr_native_value_attr_native_value = value
158  self._attr_extra_state_attributes_attr_extra_state_attributes = extra_state_attributes
159 
160  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, FullyKioskDataUpdateCoordinator coordinator, FullySensorEntityDescription sensor)
Definition: sensor.py:137
tuple[StateType, dict[str, Any]] truncate_url(StateType value)
Definition: sensor.py:30
None async_setup_entry(HomeAssistant hass, FullyKioskConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:118