DEVELOPMENT ENVIRONMENT

~liljamo/ha-ouman-eh800

ref: b818378c587f3207e22502dac05400d95850e964 ha-ouman-eh800/custom_components/ouman_eh800/select.py -rw-r--r-- 2.7 KiB
b818378cJonni Liljamo docs: update README.md 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import logging

from homeassistant.components.select import (
    SelectEntity,
    SelectEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import OumanEH800Device
from .const import DOMAIN, EVENT_CHANGE_L1_OPERATION_MODE
from .eh800 import OPERATION_MODES

_LOGGER = logging.getLogger(__name__)


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

    entities: list[OumanEH800DeviceSelect] = [
        OumanEH800DeviceSelect(
            device,
            SelectEntityDescription(
                key="l1_operation_mode",
            ),
        )
    ]

    async_add_entities(entities, True)


class OumanEH800DeviceSelect(SelectEntity):
    entity_description: SelectEntityDescription

    def __init__(
        self,
        device: OumanEH800Device,
        description: SelectEntityDescription,
    ) -> None:
        self._device = device
        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

    async def async_added_to_hass(self):
        self.hass.bus.async_listen(
            EVENT_CHANGE_L1_OPERATION_MODE, self.async_update_event_handler
        )

    async def async_update_event_handler(
        self,
        event,  # pylint: disable=unused-argument
    ):
        await self.async_update()
        self.async_write_ha_state()

    @property
    def current_option(self) -> str:
        operation_mode = int(
            self._device.device.data.get(self.entity_description.key, 0)
        )
        return [om.name for om in OPERATION_MODES if om.value == operation_mode][0]

    @property
    def options(self) -> list[str]:
        return [om.name for om in OPERATION_MODES]

    async def async_select_option(self, option: str) -> None:
        operation_mode = [om for om in OPERATION_MODES if om.name == option][0]
        _LOGGER.debug(
            "Setting operation mode to '%s' (%s)",
            operation_mode.name,
            operation_mode.value,
        )
        await self._device.device.update_value(
            self.entity_description.key,
            operation_mode.value,
        )
        self.hass.bus.async_fire(EVENT_CHANGE_L1_OPERATION_MODE)
        self.async_write_ha_state()

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