1+ """Select platform for Systemair."""
2+
3+ from __future__ import annotations
4+
5+ import asyncio
6+ import asyncio .exceptions
7+ from dataclasses import dataclass
8+ from typing import TYPE_CHECKING , Any
9+
10+ from homeassistant .components .select import SelectEntity , SelectEntityDescription
11+ from homeassistant .const import EntityCategory
12+ from homeassistant .exceptions import HomeAssistantError
13+
14+ from .entity import SystemairEntity
15+ from .modbus import ModbusParameter , parameter_map
16+
17+ if TYPE_CHECKING :
18+ from homeassistant .core import HomeAssistant
19+ from homeassistant .helpers .entity_platform import AddEntitiesCallback
20+
21+ from .coordinator import SystemairDataUpdateCoordinator
22+ from .data import SystemairConfigEntry
23+
24+
25+ @dataclass (kw_only = True , frozen = True )
26+ class SystemairSelectEntityDescription (SelectEntityDescription ):
27+ """Describes a Systemair select entity."""
28+
29+ registry : ModbusParameter
30+ options_map : dict [int , str ]
31+
32+
33+ ENTITY_DESCRIPTIONS = (
34+ SystemairSelectEntityDescription (
35+ key = "temperature_control_mode" ,
36+ translation_key = "temperature_control_mode" ,
37+ icon = "mdi:thermostat" ,
38+ entity_category = EntityCategory .CONFIG ,
39+ registry = parameter_map ["REG_TC_CONTROL_MODE" ],
40+ options_map = {
41+ 0 : "supply_air" ,
42+ 1 : "room_air" ,
43+ 2 : "extract_air" ,
44+ },
45+ ),
46+ SystemairSelectEntityDescription (
47+ key = "fan_regulation_unit" ,
48+ translation_key = "fan_regulation_unit" ,
49+ icon = "mdi:fan-speed-1" ,
50+ entity_category = EntityCategory .CONFIG ,
51+ registry = parameter_map ["REG_FAN_REGULATION_UNIT" ],
52+ options_map = {
53+ 0 : "manual_percent" ,
54+ 1 : "manual_rpm" ,
55+ 2 : "pressure" ,
56+ 3 : "flow" ,
57+ 4 : "external" ,
58+ },
59+ ),
60+ SystemairSelectEntityDescription (
61+ key = "defrosting_mode" ,
62+ translation_key = "defrosting_mode" ,
63+ icon = "mdi:snowflake-melt" ,
64+ entity_category = EntityCategory .CONFIG ,
65+ registry = parameter_map ["REG_DEFROSTING_MODE" ],
66+ options_map = {
67+ 0 : "soft" ,
68+ 1 : "normal" ,
69+ 2 : "hard" ,
70+ },
71+ ),
72+ )
73+
74+
75+ async def async_setup_entry (
76+ hass : HomeAssistant , # noqa: ARG001
77+ entry : SystemairConfigEntry ,
78+ async_add_entities : AddEntitiesCallback ,
79+ ) -> None :
80+ """Set up the select platform."""
81+ async_add_entities (
82+ SystemairSelect (
83+ coordinator = entry .runtime_data .coordinator ,
84+ entity_description = entity_description ,
85+ )
86+ for entity_description in ENTITY_DESCRIPTIONS
87+ )
88+
89+
90+ class SystemairSelect (SystemairEntity , SelectEntity ):
91+ """Systemair select class."""
92+
93+ _attr_has_entity_name = True
94+ entity_description : SystemairSelectEntityDescription
95+
96+ def __init__ (
97+ self ,
98+ coordinator : SystemairDataUpdateCoordinator ,
99+ entity_description : SystemairSelectEntityDescription ,
100+ ) -> None :
101+ """Initialize the select class."""
102+ super ().__init__ (coordinator )
103+ self .entity_description = entity_description
104+ self ._attr_unique_id = f"{ coordinator .config_entry .entry_id } -{ entity_description .key } "
105+ self ._attr_options = list (self .entity_description .options_map .values ())
106+ self ._option_to_value_map = {v : k for k , v in self .entity_description .options_map .items ()}
107+
108+ @property
109+ def current_option (self ) -> str | None :
110+ """Return the selected entity option to represent the entity state."""
111+ value = self .coordinator .get_modbus_data (self .entity_description .registry )
112+ return self .entity_description .options_map .get (int (value ))
113+
114+ async def async_select_option (self , option : str ) -> None :
115+ """Change the selected option."""
116+ value = self ._option_to_value_map .get (option )
117+ if value is None :
118+ return
119+
120+ try :
121+ await self .coordinator .set_modbus_data (self .entity_description .registry , value )
122+ await asyncio .sleep (1 )
123+ except (asyncio .exceptions .TimeoutError , ConnectionError ) as exc :
124+ raise HomeAssistantError from exc
125+ finally :
126+ await self .coordinator .async_refresh ()
0 commit comments