Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Aussie Broadband metric sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 import re
8 from typing import cast
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.const import UnitOfInformation, UnitOfTime
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.helpers.typing import StateType
21 from homeassistant.helpers.update_coordinator import CoordinatorEntity
22 
23 from .const import DOMAIN, SERVICE_ID
24 from .coordinator import (
25  AussieBroadbandConfigEntry,
26  AussieBroadbandDataUpdateCoordinator,
27  AussieBroadbandServiceData,
28 )
29 
30 
31 @dataclass(frozen=True)
33  """Class describing Aussie Broadband sensor entities."""
34 
35  value: Callable = lambda x: x
36 
37 
38 SENSOR_DESCRIPTIONS: tuple[SensorValueEntityDescription, ...] = (
39  # Internet Services sensors
41  key="usedMb",
42  translation_key="data_used",
43  state_class=SensorStateClass.TOTAL_INCREASING,
44  native_unit_of_measurement=UnitOfInformation.MEGABYTES,
45  device_class=SensorDeviceClass.DATA_SIZE,
46  ),
48  key="downloadedMb",
49  translation_key="downloaded",
50  state_class=SensorStateClass.TOTAL_INCREASING,
51  native_unit_of_measurement=UnitOfInformation.MEGABYTES,
52  device_class=SensorDeviceClass.DATA_SIZE,
53  ),
55  key="uploadedMb",
56  translation_key="uploaded",
57  state_class=SensorStateClass.TOTAL_INCREASING,
58  native_unit_of_measurement=UnitOfInformation.MEGABYTES,
59  device_class=SensorDeviceClass.DATA_SIZE,
60  ),
61  # Mobile Phone Services sensors
63  key="national",
64  translation_key="national_calls",
65  state_class=SensorStateClass.TOTAL_INCREASING,
66  value=lambda x: x.get("calls"),
67  ),
69  key="mobile",
70  translation_key="mobile_calls",
71  state_class=SensorStateClass.TOTAL_INCREASING,
72  value=lambda x: x.get("calls"),
73  ),
75  key="international",
76  translation_key="international_calls",
77  entity_registry_enabled_default=False,
78  state_class=SensorStateClass.TOTAL_INCREASING,
79  value=lambda x: x.get("calls"),
80  ),
82  key="sms",
83  translation_key="sms_sent",
84  state_class=SensorStateClass.TOTAL_INCREASING,
85  value=lambda x: x.get("calls"),
86  ),
88  key="internet",
89  translation_key="data_used",
90  state_class=SensorStateClass.TOTAL_INCREASING,
91  native_unit_of_measurement=UnitOfInformation.KILOBYTES,
92  device_class=SensorDeviceClass.DATA_SIZE,
93  value=lambda x: x.get("kbytes"),
94  ),
96  key="voicemail",
97  translation_key="voicemail_calls",
98  entity_registry_enabled_default=False,
99  state_class=SensorStateClass.TOTAL_INCREASING,
100  value=lambda x: x.get("calls"),
101  ),
103  key="other",
104  translation_key="other_calls",
105  entity_registry_enabled_default=False,
106  state_class=SensorStateClass.TOTAL_INCREASING,
107  value=lambda x: x.get("calls"),
108  ),
109  # Generic sensors
111  key="daysTotal",
112  translation_key="billing_cycle_length",
113  native_unit_of_measurement=UnitOfTime.DAYS,
114  ),
116  key="daysRemaining",
117  translation_key="billing_cycle_remaining",
118  native_unit_of_measurement=UnitOfTime.DAYS,
119  ),
120 )
121 
122 
124  hass: HomeAssistant,
125  entry: AussieBroadbandConfigEntry,
126  async_add_entities: AddEntitiesCallback,
127 ) -> None:
128  """Set up the Aussie Broadband sensor platform from a config entry."""
129 
131  [
132  AussieBroadandSensorEntity(service, description)
133  for service in entry.runtime_data
134  for description in SENSOR_DESCRIPTIONS
135  if description.key in service["coordinator"].data
136  ]
137  )
138 
139 
141  CoordinatorEntity[AussieBroadbandDataUpdateCoordinator], SensorEntity
142 ):
143  """Base class for Aussie Broadband metric sensors."""
144 
145  _attr_has_entity_name = True
146  entity_description: SensorValueEntityDescription
147 
148  def __init__(
149  self,
150  service: AussieBroadbandServiceData,
151  description: SensorValueEntityDescription,
152  ) -> None:
153  """Initialize the sensor."""
154  super().__init__(service["coordinator"])
155  self.entity_descriptionentity_description = description
156  self._attr_unique_id_attr_unique_id = f"{service[SERVICE_ID]}:{description.key}"
157  self._attr_device_info_attr_device_info = DeviceInfo(
158  entry_type=DeviceEntryType.SERVICE,
159  identifiers={(DOMAIN, service[SERVICE_ID])},
160  manufacturer="Aussie Broadband",
161  configuration_url=f"https://my.aussiebroadband.com.au/#/{service['name'].lower()}/{service[SERVICE_ID]}/",
162  name=re.sub(r" - AVC\d+$", "", service["description"]),
163  model=service["name"],
164  )
165 
166  @property
167  def native_value(self) -> StateType:
168  """Return the state of the sensor."""
169  parent = self.coordinator.data[self.entity_descriptionentity_description.key]
170  return cast(StateType, self.entity_descriptionentity_description.value(parent))
None __init__(self, AussieBroadbandServiceData service, SensorValueEntityDescription description)
Definition: sensor.py:152
None async_setup_entry(HomeAssistant hass, AussieBroadbandConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:127