sensor.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """Sensor platform for knmi."""
  2. from collections.abc import Callable
  3. from dataclasses import dataclass
  4. from datetime import datetime
  5. from typing import Any
  6. from homeassistant.components.sensor import (
  7. SensorDeviceClass,
  8. SensorEntity,
  9. SensorEntityDescription,
  10. SensorStateClass,
  11. )
  12. from homeassistant.config_entries import ConfigEntry
  13. from homeassistant.const import (
  14. CONF_NAME,
  15. PERCENTAGE,
  16. UnitOfSoundPressure,
  17. UnitOfDataRate
  18. )
  19. from homeassistant.core import HomeAssistant
  20. from homeassistant.helpers.entity import EntityCategory
  21. from homeassistant.helpers.entity_platform import AddEntitiesCallback
  22. from homeassistant.helpers.typing import StateType
  23. from homeassistant.helpers.update_coordinator import CoordinatorEntity
  24. from .const import (DOMAIN,
  25. EP_CELLINFO,
  26. EP_DEVICESTATUS,
  27. EP_LANINFO)
  28. from .coordinator import RouterDataUpdateCoordinator
  29. @dataclass(kw_only=True, frozen=True)
  30. class RouterSensorDescription(SensorEntityDescription):
  31. """Class describing Router sensor entities."""
  32. value_fn: Callable[[dict[str, Any]], StateType | datetime | None]
  33. attr_fn: Callable[[dict[str, Any]], dict[str, Any]] = lambda _: {}
  34. DESCRIPTIONS: list[RouterSensorDescription] = [
  35. RouterSensorDescription(
  36. key='rssi',
  37. icon='mdi:wifi-check',
  38. value_fn=lambda coordinator: coordinator.get_value(EP_CELLINFO, ["CellIntfInfo", "RSSI"]),
  39. native_unit_of_measurement=UnitOfSoundPressure.WEIGHTED_DECIBEL_A,
  40. device_class=SensorDeviceClass.SOUND_PRESSURE,
  41. state_class=SensorStateClass.MEASUREMENT,
  42. translation_key='rssi',
  43. entity_registry_enabled_default=True,
  44. ),
  45. RouterSensorDescription(
  46. key='rsrq',
  47. icon='mdi:wifi-arrow-up-down',
  48. value_fn=lambda coordinator: coordinator.get_value(EP_CELLINFO, ["CellIntfInfo", "X_ZYXEL_RSRQ"]),
  49. native_unit_of_measurement=UnitOfSoundPressure.DECIBEL,
  50. device_class=SensorDeviceClass.SOUND_PRESSURE,
  51. state_class=SensorStateClass.MEASUREMENT,
  52. translation_key='rsrq',
  53. entity_registry_enabled_default=False
  54. ),
  55. RouterSensorDescription(
  56. key='rsrp',
  57. icon='mdi:wifi-arrow-down',
  58. value_fn=lambda coordinator: coordinator.get_value(EP_CELLINFO, ["CellIntfInfo", "X_ZYXEL_RSRP"]),
  59. native_unit_of_measurement=UnitOfSoundPressure.WEIGHTED_DECIBEL_A,
  60. device_class=SensorDeviceClass.SOUND_PRESSURE,
  61. state_class=SensorStateClass.MEASUREMENT,
  62. translation_key='rsrp',
  63. entity_registry_enabled_default=False
  64. ),
  65. RouterSensorDescription(
  66. key='sinr',
  67. icon='mdi:wifi-alert',
  68. value_fn=lambda coordinator: coordinator.get_value(EP_CELLINFO, ["CellIntfInfo", "X_ZYXEL_SINR"]),
  69. state_class=SensorStateClass.MEASUREMENT,
  70. translation_key='sinr',
  71. entity_registry_enabled_default=False
  72. ),
  73. RouterSensorDescription(
  74. key='network_technology',
  75. icon='mdi:radio-tower',
  76. value_fn=lambda coordinator: coordinator.get_value(EP_CELLINFO, ["CellIntfInfo", "CurrentAccessTechnology"]),
  77. translation_key='network_technology',
  78. entity_registry_enabled_default=True
  79. ),
  80. RouterSensorDescription(
  81. key='network_band',
  82. icon='mdi:signal-5g',
  83. value_fn=lambda coordinator: coordinator.get_value(EP_CELLINFO, ["CellIntfInfo", "CurrentAccessTechnology"]),
  84. translation_key='network_band',
  85. entity_registry_enabled_default=False
  86. ),
  87. # RouterSensorDescription(
  88. # key='network_devices',
  89. # state_class=SensorStateClass.MEASUREMENT,
  90. # translation_key='network_devices',
  91. # entity_registry_enabled_default=True
  92. # )
  93. ]
  94. async def async_setup_entry(
  95. hass: HomeAssistant,
  96. entry: ConfigEntry,
  97. async_add_entities: AddEntitiesCallback,
  98. ) -> None:
  99. """Set up Router sensors based on a config entry."""
  100. conf_name = entry.data.get(CONF_NAME)
  101. coordinator = hass.data[DOMAIN][entry.entry_id]
  102. entities: list[RouterSensor] = []
  103. # Add all sensors described above.
  104. for description in DESCRIPTIONS:
  105. entities.append(
  106. RouterSensor(
  107. conf_name=conf_name,
  108. coordinator=coordinator,
  109. description=description,
  110. )
  111. )
  112. async_add_entities(entities)
  113. class RouterSensor(CoordinatorEntity[RouterDataUpdateCoordinator], SensorEntity):
  114. """Defines a Router sensor."""
  115. _attr_has_entity_name = True
  116. entity_description: RouterSensorDescription
  117. def __init__(
  118. self,
  119. conf_name: str,
  120. coordinator: RouterDataUpdateCoordinator,
  121. description: SensorEntityDescription,
  122. ) -> None:
  123. """Initialize Router sensor."""
  124. super().__init__(coordinator=coordinator)
  125. self._attr_attribution = self.coordinator.get_value(["api", 0, "bron"])
  126. self._attr_device_info = coordinator.device_info
  127. self._attr_unique_id = f"{conf_name}_{description.key}".lower()
  128. self.entity_description = description
  129. @property
  130. def native_value(self) -> StateType:
  131. """Return the state."""
  132. return self.entity_description.value_fn(self.coordinator)
  133. @property
  134. def extra_state_attributes(self) -> dict[str, Any]:
  135. """Return the state attributes."""
  136. return self.entity_description.attr_fn(self.coordinator)