-
-
Notifications
You must be signed in to change notification settings - Fork 28.5k
/
select.py
138 lines (119 loc) · 4.69 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
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
"""Support for Tuya select."""
from __future__ import annotations
from typing import cast
from tuya_iot import TuyaDevice, TuyaDeviceManager
from tuya_iot.device import TuyaDeviceStatusRange
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ENTITY_CATEGORY_CONFIG
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HomeAssistantTuyaData
from .base import EnumTypeData, TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode
# All descriptions can be found here. Mostly the Enum data types in the
# default instructions set of each category end up being a select.
# https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq
SELECTS: dict[str, tuple[SelectEntityDescription, ...]] = {
# Coffee maker
# https://developer.tuya.com/en/docs/iot/categorykfj?id=Kaiuz2p12pc7f
"kfj": (
SelectEntityDescription(
key=DPCode.CUP_NUMBER,
name="Cups",
icon="mdi:numeric",
),
SelectEntityDescription(
key=DPCode.CONCENTRATION_SET,
name="Concentration",
icon="mdi:altimeter",
),
SelectEntityDescription(
key=DPCode.MATERIAL,
name="Material",
),
SelectEntityDescription(
key=DPCode.MODE,
name="Mode",
),
),
# Siren Alarm
# https://developer.tuya.com/en/docs/iot/categorysgbj?id=Kaiuz37tlpbnu
"sgbj": (
SelectEntityDescription(
key=DPCode.ALARM_VOLUME,
name="Volume",
entity_category=ENTITY_CATEGORY_CONFIG,
),
SelectEntityDescription(
key=DPCode.BRIGHT_STATE,
name="Brightness",
entity_category=ENTITY_CATEGORY_CONFIG,
),
),
}
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Tuya select dynamically through Tuya discovery."""
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
@callback
def async_discover_device(device_ids: list[str]) -> None:
"""Discover and add a discovered Tuya select."""
entities: list[TuyaSelectEntity] = []
for device_id in device_ids:
device = hass_data.device_manager.device_map[device_id]
if descriptions := SELECTS.get(device.category):
for description in descriptions:
if (
description.key in device.function
or description.key in device.status
):
entities.append(
TuyaSelectEntity(
device, hass_data.device_manager, description
)
)
async_add_entities(entities)
async_discover_device([*hass_data.device_manager.device_map])
entry.async_on_unload(
async_dispatcher_connect(hass, TUYA_DISCOVERY_NEW, async_discover_device)
)
class TuyaSelectEntity(TuyaEntity, SelectEntity):
"""Tuya Select Entity."""
def __init__(
self,
device: TuyaDevice,
device_manager: TuyaDeviceManager,
description: SelectEntityDescription,
) -> None:
"""Init Tuya sensor."""
super().__init__(device, device_manager)
self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}"
self._attr_opions: list[str] = []
if status_range := device.status_range.get(description.key):
self._status_range = cast(TuyaDeviceStatusRange, status_range)
# Extract type data from enum status range,
if self._status_range.type == "Enum":
type_data = EnumTypeData.from_json(self._status_range.values)
self._attr_options = type_data.range
@property
def current_option(self) -> str | None:
"""Return the selected entity option to represent the entity state."""
# Raw value
value = self.device.status.get(self.entity_description.key)
if value is None or value not in self._attr_options:
return None
return value
def select_option(self, option: str) -> None:
"""Change the selected option."""
self._send_command(
[
{
"code": self.entity_description.key,
"value": option,
}
]
)