DEVELOPMENT ENVIRONMENT

~liljamo/ha-ouman-eh800

ref: 0442c660c5b434d3d4304d0299ba3eb586d82ac9 ha-ouman-eh800/custom_components/ouman_eh800/__init__.py -rw-r--r-- 1.9 KiB
0442c660Jonni Liljamo feat: select for operation mode 10 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
from datetime import timedelta
import logging

from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.util import Throttle

from .const import DOMAIN, CONF_HOST, CONF_PORT, CONF_USERNAME, CONF_PASSWORD
from .eh800 import EH800

_LOGGER = logging.getLogger(__name__)

PLATFORMS = [
    Platform.CLIMATE,
    Platform.NUMBER,
    Platform.SELECT,
    Platform.SENSOR,
    Platform.SWITCH,
    Platform.VALVE,
]

UPDATE_INTERVAL = timedelta(minutes=1)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    _LOGGER.debug("Setting up Ouman EH-800")

    config = dict(entry.data)
    eh800 = EH800(
        config[CONF_HOST],
        config[CONF_PORT],
        config[CONF_USERNAME],
        config[CONF_PASSWORD],
    )
    # Do an initial update straight away.
    await eh800.update()

    device = OumanEH800Device(hass, eh800, entry)

    hass.data.setdefault(DOMAIN, {}).update({entry.entry_id: device})
    await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
    return True


class OumanEH800Device:
    """Ouman EH-800 Device instance."""

    def __init__(self, hass: HomeAssistant, device: EH800, entry: ConfigEntry) -> None:
        self._hass = hass
        self.device = device
        self.entry = entry

    @Throttle(UPDATE_INTERVAL)
    async def async_update(
        self,
        **kwargs,  # pylint: disable=unused-argument
    ):
        """Pull data from Ouman EH-800."""

        update_ok = await self.device.update()
        if not update_ok:
            _LOGGER.warning("Failed to update EH-800 device!")

    @property
    def device_info(self) -> DeviceInfo:
        return DeviceInfo(
            identifiers={(DOMAIN, self.entry.entry_id)},
            manufacturer="Ouman",
            name="Ouman EH-800",
        )