Welcome to mirror list, hosted at ThFree Co, Russian Federation.

switch.py « skykettle « custom_components - github.com/ClusterM/skykettle-ha.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: df9ceb71bc3c9a6594968f9213575090ffe3e419 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""SkyKettle."""
import logging

from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.dispatcher import async_dispatcher_send, async_dispatcher_connect
from homeassistant.const import *

from .skykettle import SkyKettle
from .const import *

_LOGGER = logging.getLogger(__name__)


SWITCH_MAIN = "switch"
SWITCH_SOUND = "sound"
SWITCH_LIGHT_SYNC = "light_sync"
SWITCH_LIGHT_BOIL = "light_boil"


async def async_setup_entry(hass, entry, async_add_entities):
    """Set up the SkyKettle entry."""
    model_code = hass.data[DOMAIN][entry.entry_id][DATA_CONNECTION].model_code
    async_add_entities([
        SkySwitch(hass, entry, SWITCH_MAIN)
    ])
    if model_code in [SkyKettle.MODELS_4, SkyKettle.MODELS_5, SkyKettle.MODELS_6, SkyKettle.MODELS_7]: # RK-G2xxS, RK-M13xS, RK-M21xS, RK-M223S but not sure
        async_add_entities([
            SkySwitch(hass, entry, SWITCH_SOUND),
            SkySwitch(hass, entry, SWITCH_LIGHT_SYNC),
            SkySwitch(hass, entry, SWITCH_LIGHT_BOIL),
        ])


class SkySwitch(SwitchEntity):
    """Representation of a SkyKettle switch device."""

    def __init__(self, hass, entry, switch_type):
        """Initialize the switch device."""
        self.hass = hass
        self.entry = entry
        self.switch_type = switch_type

    async def async_added_to_hass(self):
        self.update()
        self.async_on_remove(async_dispatcher_connect(self.hass, DISPATCHER_UPDATE, self.update))

    def update(self):
        self.schedule_update_ha_state()

    @property
    def kettle(self):
        return self.hass.data[DOMAIN][self.entry.entry_id][DATA_CONNECTION]

    @property
    def unique_id(self):
        return f"{self.entry.entry_id}_{self.switch_type}"

    @property
    def name(self):
        """Name of the entity."""
        if self.switch_type == SWITCH_MAIN:
            return (FRIENDLY_NAME + " " + self.entry.data.get(CONF_FRIENDLY_NAME, "")).strip()
        if self.switch_type == SWITCH_SOUND:
            return (FRIENDLY_NAME + " " + self.entry.data.get(CONF_FRIENDLY_NAME, "")).strip() + " enable sound"
        if self.switch_type == SWITCH_LIGHT_SYNC:
            return (FRIENDLY_NAME + " " + self.entry.data.get(CONF_FRIENDLY_NAME, "")).strip() + " enable sync light"
        if self.switch_type == SWITCH_LIGHT_BOIL:
            return (FRIENDLY_NAME + " " + self.entry.data.get(CONF_FRIENDLY_NAME, "")).strip() + " enable boil light"

    @property
    def icon(self):
        if self.switch_type == SWITCH_MAIN:
            return "mdi:cog"
        if self.switch_type == SWITCH_SOUND:
            return "mdi:volume-high"
        if self.switch_type == SWITCH_LIGHT_SYNC:
            return "mdi:sync"
        if self.switch_type == SWITCH_LIGHT_BOIL:
            return "mdi:alarm-light"

    @property
    def device_class(self):
        return SwitchDeviceClass.SWITCH

    @property
    def device_info(self):
        return self.hass.data[DOMAIN][DATA_DEVICE_INFO]()

    @property
    def should_poll(self):
        return False

    @property
    def assumed_state(self):
        return False

    @property
    def available(self):        
        if self.switch_type == SWITCH_MAIN:
            return self.kettle.available
        if self.switch_type == SWITCH_SOUND:
            return self.kettle.available and self.kettle.sound_enabled != None
        if self.switch_type == SWITCH_LIGHT_SYNC:
            return self.kettle.available and self.kettle.light_switch_sync != None
        if self.switch_type == SWITCH_LIGHT_BOIL:
            return self.kettle.available and self.kettle.light_switch_boil != None

    @property
    def entity_category(self):
        if self.switch_type == SWITCH_MAIN:
            return None
        if self.switch_type == SWITCH_SOUND:
            return EntityCategory.CONFIG
        if self.switch_type == SWITCH_LIGHT_SYNC:
            return EntityCategory.CONFIG
        if self.switch_type == SWITCH_LIGHT_BOIL:
            return EntityCategory.CONFIG

    @property
    def is_on(self):
        """If the switch is currently on or off."""
        if self.switch_type == SWITCH_MAIN:
            return self.kettle.target_mode != None
        if self.switch_type == SWITCH_SOUND:
            return self.kettle.sound_enabled
        if self.switch_type == SWITCH_LIGHT_SYNC:
            return self.kettle.light_switch_sync
        if self.switch_type == SWITCH_LIGHT_BOIL:
            return self.kettle.light_switch_boil

    async def async_turn_on(self, **kwargs):
        """Turn the switch on."""
        if self.switch_type == SWITCH_MAIN:
            await self.kettle.set_target_mode(SkyKettle.MODE_NAMES[SkyKettle.MODE_BOIL])
        if self.switch_type == SWITCH_SOUND:
            await self.kettle.set_sound(True)
        if self.switch_type == SWITCH_LIGHT_SYNC:
            await self.kettle.set_light_switch(SkyKettle.LIGHT_SYNC, True)
        if self.switch_type == SWITCH_LIGHT_BOIL:
            await self.kettle.set_light_switch(SkyKettle.LIGHT_BOIL, True)
        self.hass.async_add_executor_job(async_dispatcher_send, self.hass, DISPATCHER_UPDATE)

    async def async_turn_off(self, **kwargs):
        """Turn the switch off."""
        if self.switch_type == SWITCH_MAIN:
            await self.kettle.set_target_mode(None)
        if self.switch_type == SWITCH_SOUND:
            await self.kettle.set_sound(False)
        if self.switch_type == SWITCH_LIGHT_SYNC:
            await self.kettle.set_light_switch(SkyKettle.LIGHT_SYNC, False)
        if self.switch_type == SWITCH_LIGHT_BOIL:
            await self.kettle.set_light_switch(SkyKettle.LIGHT_BOIL, False)
        self.hass.async_add_executor_job(async_dispatcher_send, self.hass, DISPATCHER_UPDATE)