DEVELOPMENT ENVIRONMENT

~liljamo/ha-ouman-eh800

ref: a21df4529c0abd305baa27231561fa0a8b1ae0b2 ha-ouman-eh800/custom_components/ouman_eh800/sensor.py -rw-r--r-- 2.7 KiB
a21df452Jonni Liljamo initial 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
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 import Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import Throttle

from. const import *
from .eh800 import EH800

_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(
        hass: HomeAssistant,
        entry: ConfigEntry,
        async_add_entities: AddEntitiesCallback,
    ) -> None:
    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):
    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):
        return SensorDeviceClass.TEMPERATURE

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

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

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

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

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

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

    @Throttle(timedelta(minutes=1))
    async def async_update(self):
        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()