1 """Fully Kiosk Browser sensor."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
12 SensorEntityDescription,
20 from .
import FullyKioskConfigEntry
21 from .coordinator
import FullyKioskDataUpdateCoordinator
22 from .entity
import FullyKioskEntity
26 """Convert storage values from bytes to megabytes."""
27 return round(value * 0.000001, 1)
30 def truncate_url(value: StateType) -> tuple[StateType, dict[str, Any]]:
31 """Truncate URL if longer than 256."""
33 truncated = len(url) > 256
34 extra_state_attributes = {
36 "truncated": truncated,
39 return (url[0:255], extra_state_attributes)
40 return (url, extra_state_attributes)
43 @dataclass(frozen=True)
45 """Fully Kiosk Browser sensor description."""
47 round_state_value: bool =
False
48 state_fn: Callable[[StateType], tuple[StateType, dict[str, Any]]] |
None =
None
51 SENSORS: tuple[FullySensorEntityDescription, ...] = (
54 device_class=SensorDeviceClass.BATTERY,
55 native_unit_of_measurement=PERCENTAGE,
56 state_class=SensorStateClass.MEASUREMENT,
57 entity_category=EntityCategory.DIAGNOSTIC,
61 translation_key=
"current_page",
62 entity_category=EntityCategory.DIAGNOSTIC,
63 state_fn=truncate_url,
66 key=
"screenOrientation",
67 translation_key=
"screen_orientation",
68 entity_category=EntityCategory.DIAGNOSTIC,
72 translation_key=
"foreground_app",
73 entity_category=EntityCategory.DIAGNOSTIC,
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,
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,
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,
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,
116 config_entry: FullyKioskConfigEntry,
117 async_add_entities: AddEntitiesCallback,
119 """Set up the Fully Kiosk Browser sensor."""
120 coordinator = config_entry.runtime_data
123 for description
in SENSORS
124 if description.key
in coordinator.data
129 """Representation of a Fully Kiosk Browser sensor."""
131 entity_description: FullySensorEntityDescription
135 coordinator: FullyKioskDataUpdateCoordinator,
136 sensor: FullySensorEntityDescription,
138 """Initialize the sensor entity."""
147 extra_state_attributes: dict[str, Any] = {}
150 if value
is not None:
152 value, extra_state_attributes = self.
entity_descriptionentity_description.state_fn(value)
None _handle_coordinator_update(self)
None __init__(self, FullyKioskDataUpdateCoordinator coordinator, FullySensorEntityDescription sensor)
_attr_extra_state_attributes
None async_write_ha_state(self)
tuple[StateType, dict[str, Any]] truncate_url(StateType value)
None async_setup_entry(HomeAssistant hass, FullyKioskConfigEntry config_entry, AddEntitiesCallback async_add_entities)
float round_storage(int value)