Files
Willem-Jan van RootselaarandJoost Lekkerkerker 1ae1391cb9 Add platform sensor to BSBLAN integration (#125474)
* add sensor platform

* refactor: Add sensor data to async_get_config_entry_diagnostics

* refactor: Add tests for sensor

* chore: remove duplicate test

* Update tests/components/bsblan/test_sensor.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* refactor: let hass use translation_key

fix raise

* refactor: Add new sensor entity names to strings.json

* refactor: Add tests for current temperature sensor

* refactor: Update native_value method in BSBLanSensor

* refactor: Update test

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-09-13 14:04:00 +02:00

85 lines
2.7 KiB
Python

"""Support for BSB-Lan sensors."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from . import BSBLanData
from .const import DOMAIN
from .coordinator import BSBLanCoordinatorData
from .entity import BSBLanEntity
@dataclass(frozen=True, kw_only=True)
class BSBLanSensorEntityDescription(SensorEntityDescription):
"""Describes BSB-Lan sensor entity."""
value_fn: Callable[[BSBLanCoordinatorData], StateType]
SENSOR_TYPES: tuple[BSBLanSensorEntityDescription, ...] = (
BSBLanSensorEntityDescription(
key="current_temperature",
translation_key="current_temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data.sensor.current_temperature.value,
),
BSBLanSensorEntityDescription(
key="outside_temperature",
translation_key="outside_temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data.sensor.outside_temperature.value,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up BSB-Lan sensor based on a config entry."""
data: BSBLanData = hass.data[DOMAIN][entry.entry_id]
async_add_entities(BSBLanSensor(data, description) for description in SENSOR_TYPES)
class BSBLanSensor(BSBLanEntity, SensorEntity):
"""Defines a BSB-Lan sensor."""
entity_description: BSBLanSensorEntityDescription
def __init__(
self,
data: BSBLanData,
description: BSBLanSensorEntityDescription,
) -> None:
"""Initialize BSB-Lan sensor."""
super().__init__(data.coordinator, data)
self.entity_description = description
self._attr_unique_id = f"{data.device.MAC}-{description.key}"
@property
def native_value(self) -> StateType:
"""Return the state of the sensor."""
value = self.entity_description.value_fn(self.coordinator.data)
if value == "---":
return None
return value