DEVELOPMENT ENVIRONMENT

~liljamo/ha-ouman-eh800

ref: 084a34e5eb98102d3beeff54d9c1368155cec938 ha-ouman-eh800/custom_components/ouman_eh800/sensor.py -rw-r--r-- 3.1 KiB
084a34e5Jonni Liljamo format 19 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
TODO
"""

import logging

from datetime import timedelta

from homeassistant.components.sensor import (
    SensorDeviceClass,
    SensorEntity,
    SensorStateClass,
)
from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import Throttle

from .const import (
    DOMAIN,
    CONF_HOST,
    CONF_PORT,
    CONF_USERNAME,
    CONF_PASSWORD,
    TEMPERATURE_SENSOR_TYPE_L1_SUPPLY,
    TEMPERATURE_SENSOR_TYPE_L1_ROOM,
    TEMPERATURE_SENSOR_TYPE_OUTSIDE,
)
from .eh800 import EH800

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
    hass: HomeAssistant,
    entry: ConfigEntry,
    async_add_entities: AddEntitiesCallback,
) -> None:
    """TODO"""
    config = hass.data[DOMAIN][entry.entry_id]

    eh800 = EH800(
        config[CONF_HOST],
        config[CONF_PORT],
        config[CONF_USERNAME],
        config[CONF_PASSWORD],
    )

    entities = []
    entities.append(TemperatureSensor(hass, eh800, TEMPERATURE_SENSOR_TYPE_OUTSIDE))
    entities.append(TemperatureSensor(hass, eh800, TEMPERATURE_SENSOR_TYPE_L1_ROOM))
    entities.append(TemperatureSensor(hass, eh800, TEMPERATURE_SENSOR_TYPE_L1_SUPPLY))

    async_add_entities(entities)

    return True


class TemperatureSensor(SensorEntity):
    """Temperature sensor"""

    def __init__(self, hass: HomeAssistant, api: EH800, sensor_type: str):
        self._hass = hass
        self._api = api
        self._sensor_type = sensor_type

        self._unique_id = f"{DOMAIN}_temperature_{sensor_type}".lower()

        self._state = 0.0

    @property
    def device_class(self):
        """TODO"""
        return SensorDeviceClass.TEMPERATURE

    @property
    def device_info(self):
        """TODO"""
        return {
            "identifiers": {(DOMAIN, self.unique_id)},
            "name": self.name,
        }

    @property
    def name(self):
        """TODO"""
        return self.unique_id

    @property
    def native_unit_of_measurement(self):
        """TODO"""
        return UnitOfTemperature.CELSIUS

    @property
    def state(self):
        """TODO"""
        return self._state

    @property
    def state_class(self):
        """TODO"""
        return SensorStateClass.MEASUREMENT

    @property
    def unique_id(self):
        """TODO"""
        return self._unique_id

    @Throttle(timedelta(minutes=1))
    async def async_update(self):
        """TODO"""
        if self._sensor_type == TEMPERATURE_SENSOR_TYPE_OUTSIDE:
            await self._hass.async_add_executor_job(self._api.update_outside_temp)
            self._state = self._api.get_outside_temp()
        elif self._sensor_type == TEMPERATURE_SENSOR_TYPE_L1_ROOM:
            await self._hass.async_add_executor_job(self._api.update_l1_room_temp)
            self._state = self._api.get_l1_room_temp()
        elif self._sensor_type == TEMPERATURE_SENSOR_TYPE_L1_SUPPLY:
            await self._hass.async_add_executor_job(self._api.update_l1_supply_temp)
            self._state = self._api.get_l1_supply_temp()