DEVELOPMENT ENVIRONMENT

~liljamo/ha-ouman-eh800

ref: 0251b07e2f6a59e89e22194e5687abf2f56ad24c ha-ouman-eh800/custom_components/ouman_eh800/switch.py -rw-r--r-- 2.1 KiB
0251b07eJonni Liljamo feat: home/away switch 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
import logging

from homeassistant.components.switch import (
    SwitchDeviceClass,
    SwitchEntity,
    SwitchEntityDescription,
)
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

_LOGGER = logging.getLogger(__name__)


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

    entities: list[OumanEH800DeviceHomeAwaySwitch] = [
        OumanEH800DeviceHomeAwaySwitch(
            device,
            SwitchEntityDescription(
                key="home_away",
                device_class=SwitchDeviceClass.SWITCH,
            ),
        )
    ]

    async_add_entities(entities, True)


class OumanEH800DeviceHomeAwaySwitch(SwitchEntity):
    entity_description: SwitchEntityDescription

    def __init__(
        self,
        device: OumanEH800Device,
        description: SwitchEntityDescription,
    ) -> 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 is_on(self) -> bool:
        value = int(self._device.device.data.get(self.entity_description.key))
        if value > 0:
            return False
        return True

    async def async_turn_off(
        self,
        **kwargs,  # pylint: disable=unused-argument
    ):
        await self._device.device.update_value(self.entity_description.key, 1)
        self.async_write_ha_state()

    async def async_turn_on(
        self,
        **kwargs,  # pylint: disable=unused-argument
    ):
        await self._device.device.update_value(self.entity_description.key, 0)
        self.async_write_ha_state()

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