Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Netgear LTE sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from eternalegypt.eternalegypt import Information
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14 )
15 from homeassistant.const import (
16  PERCENTAGE,
17  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
18  EntityCategory,
19  UnitOfInformation,
20 )
21 from homeassistant.core import HomeAssistant
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
23 from homeassistant.helpers.typing import StateType
24 
25 from . import NetgearLTEConfigEntry
26 from .entity import LTEEntity
27 
28 
29 @dataclass(frozen=True, kw_only=True)
31  """Class describing Netgear LTE entities."""
32 
33  value_fn: Callable[[Information], StateType] | None = None
34 
35 
36 SENSORS: tuple[NetgearLTESensorEntityDescription, ...] = (
38  key="sms",
39  translation_key="sms",
40  native_unit_of_measurement="unread",
41  value_fn=lambda data: sum(1 for x in data.sms if x.unread),
42  ),
44  key="sms_total",
45  translation_key="sms_total",
46  native_unit_of_measurement="messages",
47  value_fn=lambda data: len(data.sms),
48  ),
50  key="usage",
51  translation_key="usage",
52  device_class=SensorDeviceClass.DATA_SIZE,
53  entity_registry_enabled_default=False,
54  native_unit_of_measurement=UnitOfInformation.BYTES,
55  suggested_unit_of_measurement=UnitOfInformation.MEBIBYTES,
56  suggested_display_precision=1,
57  value_fn=lambda data: data.usage,
58  ),
60  key="radio_quality",
61  translation_key="radio_quality",
62  entity_registry_enabled_default=False,
63  entity_category=EntityCategory.DIAGNOSTIC,
64  native_unit_of_measurement=PERCENTAGE,
65  ),
67  key="rx_level",
68  translation_key="rx_level",
69  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
70  entity_registry_enabled_default=False,
71  entity_category=EntityCategory.DIAGNOSTIC,
72  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
73  ),
75  key="tx_level",
76  translation_key="tx_level",
77  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
78  entity_registry_enabled_default=False,
79  entity_category=EntityCategory.DIAGNOSTIC,
80  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
81  ),
83  key="upstream",
84  translation_key="upstream",
85  entity_registry_enabled_default=False,
86  entity_category=EntityCategory.DIAGNOSTIC,
87  ),
89  key="connection_text",
90  translation_key="connection_text",
91  entity_registry_enabled_default=False,
92  entity_category=EntityCategory.DIAGNOSTIC,
93  ),
95  key="connection_type",
96  translation_key="connection_type",
97  entity_registry_enabled_default=False,
98  entity_category=EntityCategory.DIAGNOSTIC,
99  ),
101  key="current_ps_service_type",
102  translation_key="service_type",
103  entity_registry_enabled_default=False,
104  entity_category=EntityCategory.DIAGNOSTIC,
105  ),
107  key="register_network_display",
108  translation_key="register_network_display",
109  entity_registry_enabled_default=False,
110  entity_category=EntityCategory.DIAGNOSTIC,
111  ),
113  key="current_band",
114  translation_key="band",
115  entity_registry_enabled_default=False,
116  entity_category=EntityCategory.DIAGNOSTIC,
117  ),
119  key="cell_id",
120  translation_key="cell_id",
121  entity_registry_enabled_default=False,
122  entity_category=EntityCategory.DIAGNOSTIC,
123  ),
124 )
125 
126 
128  hass: HomeAssistant,
129  entry: NetgearLTEConfigEntry,
130  async_add_entities: AddEntitiesCallback,
131 ) -> None:
132  """Set up the Netgear LTE sensor."""
133  async_add_entities(NetgearLTESensor(entry, description) for description in SENSORS)
134 
135 
137  """Base LTE sensor entity."""
138 
139  entity_description: NetgearLTESensorEntityDescription
140 
141  @property
142  def native_value(self) -> StateType:
143  """Return the state of the sensor."""
144  if self.entity_descriptionentity_description.value_fn is not None:
145  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
146  return getattr(self.coordinator.data, self.entity_descriptionentity_description.key)
None async_setup_entry(HomeAssistant hass, NetgearLTEConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:131