DEVELOPMENT ENVIRONMENT

~liljamo/ha-ouman-eh800

ref: 0442c660c5b434d3d4304d0299ba3eb586d82ac9 ha-ouman-eh800/custom_components/ouman_eh800/climate.py -rw-r--r-- 4.2 KiB
0442c660Jonni Liljamo feat: select for operation mode 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import logging

from homeassistant.components.climate import (
    ClimateEntity,
    ClimateEntityDescription,
    ClimateEntityFeature,
    HVACMode,
)
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
from .eh800 import OPERATION_MODES

_LOGGER = logging.getLogger(__name__)


class OumanEH800DeviceClimateEntityDescription(
    ClimateEntityDescription, frozen_or_thawed=True
):  # pylint: disable=too-few-public-methods
    current_temperature_key: str
    target_temperature_key: str
    operation_mode_key: str


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[OumanEH800DeviceClimate] = [
        OumanEH800DeviceClimate(
            device,
            OumanEH800DeviceClimateEntityDescription(
                key="l1_climate",
                current_temperature_key="l1_room_temperature",
                target_temperature_key="l1_target_room_temperature",
                operation_mode_key="l1_operation_mode",
            ),
        )
    ]

    async_add_entities(entities, True)


class OumanEH800DeviceClimate(ClimateEntity):
    entity_description: OumanEH800DeviceClimateEntityDescription

    _attr_supported_features = (
        ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
    )
    _attr_temperature_unit = UnitOfTemperature.CELSIUS

    def __init__(
        self,
        device: OumanEH800Device,
        description: OumanEH800DeviceClimateEntityDescription,
    ) -> 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

    @property
    def extra_state_attributes(self) -> dict:
        return self._device.device.data

    @property
    def hvac_mode(self) -> HVACMode:
        operation_mode = int(
            self._device.device.data.get(self.entity_description.operation_mode_key, 0)
        )
        if operation_mode == 5:
            return HVACMode.OFF
        if operation_mode == 0:
            return HVACMode.AUTO
        return HVACMode.HEAT

    @property
    def hvac_modes(self) -> list[HVACMode]:
        return []

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

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

    @property
    def current_temperature(self) -> float:
        return float(
            self._device.device.data.get(
                self.entity_description.current_temperature_key, 0.0
            )
        )

    @property
    def target_temperature(self) -> float:
        return float(
            self._device.device.data.get(
                self.entity_description.target_temperature_key, 0.0
            )
        )

    async def async_set_temperature(self, **kwargs) -> None:
        await self._device.device.update_value(
            self.entity_description.target_temperature_key,
            kwargs.get("temperature", self.target_temperature),
        )
        self.async_write_ha_state()

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

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