-
Notifications
You must be signed in to change notification settings - Fork 16
/
select.py
58 lines (46 loc) · 1.7 KB
/
select.py
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
"""Support for select."""
import logging
import asyncio
from homeassistant.core import HomeAssistant
from homeassistant.components.select import (
SelectEntity,
DOMAIN as ENTITY_DOMAIN,
)
from . import (
DOMAIN,
PetkitDevice,
PetkitEntity,
async_setup_accounts,
)
_LOGGER = logging.getLogger(__name__)
DATA_KEY = f'{ENTITY_DOMAIN}.{DOMAIN}'
async def async_setup_entry(hass, config_entry, async_add_entities):
cfg = {**config_entry.data, **config_entry.options}
await async_setup_platform(hass, cfg, async_setup_platform, async_add_entities)
async def async_setup_platform(hass: HomeAssistant, config, async_add_entities, discovery_info=None):
hass.data[DOMAIN]['add_entities'][ENTITY_DOMAIN] = async_add_entities
await async_setup_accounts(hass, ENTITY_DOMAIN)
class PetkitSelectEntity(PetkitEntity, SelectEntity):
def __init__(self, name, device: PetkitDevice, option=None):
super().__init__(name, device, option)
self._attr_current_option = None
self._attr_options = self._option.get('options')
def update(self):
super().update()
self._attr_current_option = self._attr_state
async def async_select_option(self, option: str):
"""Change the selected option."""
ret = False
fun = self._option.get('async_select')
if callable(fun):
kws = {
'entity': self,
}
ret = await fun(option, **kws)
if ret:
self._attr_current_option = option
self.async_write_ha_state()
if dly := self._option.get('delay_update'):
await asyncio.sleep(dly)
self._handle_coordinator_update()
return ret