1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-22 08:20:04 +01:00
Files
core/homeassistant/components/thethingsnetwork/sensor.py
T
epenet 7acc412902 Use runtime_data in thethingsnetwork integration (#168589)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-20 10:28:36 +02:00

60 lines
1.7 KiB
Python

"""The Things Network's integration sensors."""
import logging
from ttn_client import TTNSensorValue
from homeassistant.components.sensor import SensorEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.typing import StateType
from .const import CONF_APP_ID
from .coordinator import TTNConfigEntry
from .entity import TTNEntity
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
entry: TTNConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Add entities for TTN."""
coordinator = entry.runtime_data
sensors: set[tuple[str, str]] = set()
def _async_measurement_listener() -> None:
data = coordinator.data
new_sensors = {
(device_id, field_id): TtnDataSensor(
coordinator,
entry.data[CONF_APP_ID],
ttn_value,
)
for device_id, device_uplinks in data.items()
for field_id, ttn_value in device_uplinks.items()
if (device_id, field_id) not in sensors
and isinstance(ttn_value, TTNSensorValue)
}
if new_sensors:
async_add_entities(new_sensors.values())
sensors.update(new_sensors.keys())
entry.async_on_unload(coordinator.async_add_listener(_async_measurement_listener))
_async_measurement_listener()
class TtnDataSensor(TTNEntity, SensorEntity):
"""Represents a TTN Home Assistant Sensor."""
_ttn_value: TTNSensorValue
@property
def native_value(self) -> StateType:
"""Return the state of the entity."""
return self._ttn_value.value