mirror of
https://github.com/home-assistant/core.git
synced 2025-04-27 10:47:51 +00:00

* thethingsnetwork upgrade to v3 * add en translations and requirements_all * fix most of the findings * hassfest * use ttn_client v0.0.3 * reduce content of initial release * remove features that trigger errors * remove unneeded * add initial testcases * Exception warning * add strict type checking * add strict type checking * full coverage * rename to conftest * review changes * avoid using private attributes - use protected instead * simplify config_flow * remove unused options * review changes * upgrade client * add types client library - no need to cast * use add_suggested_values_to_schema * add ruff fix * review changes * remove unneeded comment * use typevar for TTN value * use typevar for TTN value * review * ruff error not detected in local * test review * re-order fixture * fix test * reviews * fix case * split testcases * review feedback * Update homeassistant/components/thethingsnetwork/__init__.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/thethingsnetwork/__init__.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update tests/components/thethingsnetwork/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove deprecated var * Update tests/components/thethingsnetwork/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Remove unused import * fix ruff warning --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
"""The Things Network's integration sensors."""
|
|
|
|
import logging
|
|
|
|
from ttn_client import TTNSensorValue
|
|
|
|
from homeassistant.components.sensor import SensorEntity, StateType
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from .const import CONF_APP_ID, DOMAIN
|
|
from .entity import TTNEntity
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
) -> None:
|
|
"""Add entities for TTN."""
|
|
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
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 len(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
|