DEVELOPMENT ENVIRONMENT

~liljamo/ha-ouman-eh800

ref: 54296dc9a00977cdc6106849241a664d00975db9 ha-ouman-eh800/custom_components/ouman_eh800/sensor.py -rw-r--r-- 2.0 KiB
54296dc9Jonni Liljamo docs: add README.md 11 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import logging

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 . import OumanEH800Device
from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)


TEMPERATURE_SENSORS: tuple[str, ...] = (
    "outside_temperature",
    "l1_supply_temperature",
    "l1_room_temperature",
    "l1_tmrsp",
)


async def async_setup_entry(
    hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
    """Set up Ouman EH-800 device sensors."""
    device = hass.data[DOMAIN].get(entry.entry_id)

    entities: list[OumanEH800DeviceSensor] = [
        OumanEH800DeviceSensor(
            device,
            sensor,
            SensorEntityDescription(
                key=sensor,
                native_unit_of_measurement=UnitOfTemperature.CELSIUS,
                device_class=SensorDeviceClass.TEMPERATURE,
                state_class=SensorStateClass.MEASUREMENT,
            ),
        )
        for sensor in TEMPERATURE_SENSORS
    ]

    async_add_entities(entities, True)


class OumanEH800DeviceSensor(SensorEntity):
    entity_description: SensorEntityDescription

    def __init__(
        self,
        device: OumanEH800Device,
        value_key: str,
        description: SensorEntityDescription,
    ) -> None:
        self._device = device
        self._value_key = value_key
        self.entity_description = description

        self._attr_name = description.key.replace("_", " ").capitalize()
        self._attr_unique_id = f"ouman_eh800_{description.key}"
        self._attr_device_info = device.device_info

    @property
    def native_value(self) -> float:
        return self._device.device.data.get(self._value_key, 0.0)

    async def async_update(self) -> None:
        await self._device.async_update()