2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Netgear LTE sensors."""
|
2022-01-12 11:53:50 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-12-27 12:39:01 +00:00
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2023-12-26 04:19:28 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-12-27 12:39:01 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
PERCENTAGE,
|
|
|
|
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
|
|
|
UnitOfInformation,
|
|
|
|
)
|
2022-01-12 11:53:50 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-12-27 12:39:01 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2023-12-27 12:39:01 +00:00
|
|
|
from . import ModemData
|
2023-12-26 04:19:28 +00:00
|
|
|
from .const import DOMAIN
|
2023-05-12 11:29:56 +00:00
|
|
|
from .entity import LTEEntity
|
2023-12-27 12:39:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
|
|
class NetgearLTESensorEntityDescription(SensorEntityDescription):
|
|
|
|
"""Class describing Netgear LTE entities."""
|
|
|
|
|
|
|
|
value_fn: Callable[[ModemData], StateType] | None = None
|
|
|
|
|
|
|
|
|
|
|
|
SENSORS: tuple[NetgearLTESensorEntityDescription, ...] = (
|
|
|
|
NetgearLTESensorEntityDescription(
|
|
|
|
key="sms",
|
2023-12-29 10:07:56 +00:00
|
|
|
translation_key="sms",
|
2023-12-27 12:39:01 +00:00
|
|
|
native_unit_of_measurement="unread",
|
|
|
|
value_fn=lambda modem_data: sum(1 for x in modem_data.data.sms if x.unread),
|
|
|
|
),
|
|
|
|
NetgearLTESensorEntityDescription(
|
|
|
|
key="sms_total",
|
2023-12-29 10:07:56 +00:00
|
|
|
translation_key="sms_total",
|
2023-12-27 12:39:01 +00:00
|
|
|
native_unit_of_measurement="messages",
|
|
|
|
value_fn=lambda modem_data: len(modem_data.data.sms),
|
|
|
|
),
|
|
|
|
NetgearLTESensorEntityDescription(
|
|
|
|
key="usage",
|
2023-12-29 10:07:56 +00:00
|
|
|
translation_key="usage",
|
2023-12-27 12:39:01 +00:00
|
|
|
device_class=SensorDeviceClass.DATA_SIZE,
|
|
|
|
native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
|
|
|
|
value_fn=lambda modem_data: round(modem_data.data.usage / 1024**2, 1),
|
|
|
|
),
|
|
|
|
NetgearLTESensorEntityDescription(
|
|
|
|
key="radio_quality",
|
2023-12-29 10:07:56 +00:00
|
|
|
translation_key="radio_quality",
|
2023-12-27 12:39:01 +00:00
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
),
|
|
|
|
NetgearLTESensorEntityDescription(
|
|
|
|
key="rx_level",
|
2023-12-29 10:07:56 +00:00
|
|
|
translation_key="rx_level",
|
2023-12-27 12:39:01 +00:00
|
|
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
|
|
|
),
|
|
|
|
NetgearLTESensorEntityDescription(
|
|
|
|
key="tx_level",
|
2023-12-29 10:07:56 +00:00
|
|
|
translation_key="tx_level",
|
2023-12-27 12:39:01 +00:00
|
|
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
|
|
|
),
|
2023-12-29 10:07:56 +00:00
|
|
|
NetgearLTESensorEntityDescription(key="upstream", translation_key="upstream"),
|
|
|
|
NetgearLTESensorEntityDescription(
|
|
|
|
key="connection_text", translation_key="connection_text"
|
|
|
|
),
|
|
|
|
NetgearLTESensorEntityDescription(
|
|
|
|
key="connection_type", translation_key="connection_type"
|
|
|
|
),
|
|
|
|
NetgearLTESensorEntityDescription(
|
|
|
|
key="current_ps_service_type", translation_key="service_type"
|
|
|
|
),
|
|
|
|
NetgearLTESensorEntityDescription(
|
|
|
|
key="register_network_display", translation_key="register_network_display"
|
|
|
|
),
|
|
|
|
NetgearLTESensorEntityDescription(key="current_band", translation_key="band"),
|
|
|
|
NetgearLTESensorEntityDescription(key="cell_id", translation_key="cell_id"),
|
2023-12-26 04:19:28 +00:00
|
|
|
)
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
|
2023-12-26 04:19:28 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2022-01-12 11:53:50 +00:00
|
|
|
) -> None:
|
2023-12-26 04:19:28 +00:00
|
|
|
"""Set up the Netgear LTE sensor."""
|
|
|
|
modem_data = hass.data[DOMAIN].get_modem_data(entry.data)
|
2019-03-22 13:43:39 +00:00
|
|
|
|
2023-12-28 16:47:04 +00:00
|
|
|
async_add_entities(
|
|
|
|
NetgearLTESensor(entry, modem_data, sensor) for sensor in SENSORS
|
|
|
|
)
|
2018-06-08 05:46:34 +00:00
|
|
|
|
|
|
|
|
2023-12-27 12:39:01 +00:00
|
|
|
class NetgearLTESensor(LTEEntity, SensorEntity):
|
2018-09-08 21:22:41 +00:00
|
|
|
"""Base LTE sensor entity."""
|
2018-06-08 05:46:34 +00:00
|
|
|
|
2023-12-27 12:39:01 +00:00
|
|
|
entity_description: NetgearLTESensorEntityDescription
|
2019-04-01 06:47:29 +00:00
|
|
|
|
|
|
|
@property
|
2023-12-27 12:39:01 +00:00
|
|
|
def native_value(self) -> StateType:
|
2019-04-01 06:47:29 +00:00
|
|
|
"""Return the state of the sensor."""
|
2023-12-27 12:39:01 +00:00
|
|
|
if self.entity_description.value_fn is not None:
|
|
|
|
return self.entity_description.value_fn(self.modem_data)
|
2023-12-28 16:47:04 +00:00
|
|
|
return getattr(self.modem_data.data, self.entity_description.key)
|