|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | from dataclasses import dataclass |
6 | | -from typing import Final |
| 6 | +from typing import TYPE_CHECKING, Final |
7 | 7 |
|
8 | 8 | from aioshelly.const import RPC_GENERATIONS |
9 | 9 |
|
|
37 | 37 | class RpcSelectDescription(RpcEntityDescription, SelectEntityDescription): |
38 | 38 | """Class to describe a RPC select entity.""" |
39 | 39 |
|
| 40 | + method: str |
| 41 | + |
| 42 | + |
| 43 | +class RpcSelect(ShellyRpcAttributeEntity, SelectEntity): |
| 44 | + """Represent a RPC select entity.""" |
| 45 | + |
| 46 | + entity_description: RpcSelectDescription |
| 47 | + _id: int |
| 48 | + |
| 49 | + def __init__( |
| 50 | + self, |
| 51 | + coordinator: ShellyRpcCoordinator, |
| 52 | + key: str, |
| 53 | + attribute: str, |
| 54 | + description: RpcSelectDescription, |
| 55 | + ) -> None: |
| 56 | + """Initialize select.""" |
| 57 | + super().__init__(coordinator, key, attribute, description) |
| 58 | + |
| 59 | + if self.option_map: |
| 60 | + self._attr_options = list(self.option_map.values()) |
| 61 | + |
| 62 | + if hasattr(self, "_attr_name") and description.role != ROLE_GENERIC: |
| 63 | + delattr(self, "_attr_name") |
| 64 | + |
| 65 | + @property |
| 66 | + def current_option(self) -> str | None: |
| 67 | + """Return the selected entity option to represent the entity state.""" |
| 68 | + if isinstance(self.attribute_value, str) and self.option_map: |
| 69 | + return self.option_map[self.attribute_value] |
| 70 | + |
| 71 | + return None |
| 72 | + |
| 73 | + @rpc_call |
| 74 | + async def async_select_option(self, option: str) -> None: |
| 75 | + """Change the value.""" |
| 76 | + method = getattr(self.coordinator.device, self.entity_description.method) |
| 77 | + |
| 78 | + if TYPE_CHECKING: |
| 79 | + assert method is not None |
| 80 | + |
| 81 | + if self.reversed_option_map: |
| 82 | + await method(self._id, self.reversed_option_map[option]) |
| 83 | + else: |
| 84 | + await method(self._id, option) |
| 85 | + |
| 86 | + |
| 87 | +class RpcCuryModeSelect(RpcSelect): |
| 88 | + """Represent a RPC select entity for Cury modes.""" |
| 89 | + |
| 90 | + @property |
| 91 | + def current_option(self) -> str | None: |
| 92 | + """Return the selected entity option to represent the entity state.""" |
| 93 | + if self.attribute_value is None: |
| 94 | + return "none" |
| 95 | + |
| 96 | + if TYPE_CHECKING: |
| 97 | + assert isinstance(self.attribute_value, str) |
| 98 | + |
| 99 | + return self.attribute_value |
| 100 | + |
40 | 101 |
|
41 | 102 | RPC_SELECT_ENTITIES: Final = { |
| 103 | + "cury_mode": RpcSelectDescription( |
| 104 | + key="cury", |
| 105 | + sub_key="mode", |
| 106 | + translation_key="cury_mode", |
| 107 | + options=[ |
| 108 | + "hall", |
| 109 | + "bedroom", |
| 110 | + "living_room", |
| 111 | + "lavatory_room", |
| 112 | + "none", |
| 113 | + "reception", |
| 114 | + "workplace", |
| 115 | + ], |
| 116 | + method="cury_set_mode", |
| 117 | + entity_class=RpcCuryModeSelect, |
| 118 | + ), |
42 | 119 | "enum_generic": RpcSelectDescription( |
43 | 120 | key="enum", |
44 | 121 | sub_key="value", |
45 | 122 | removal_condition=lambda config, _status, key: not is_view_for_platform( |
46 | 123 | config, key, SELECT_PLATFORM |
47 | 124 | ), |
| 125 | + method="enum_set", |
48 | 126 | role=ROLE_GENERIC, |
49 | 127 | ), |
50 | 128 | } |
@@ -89,37 +167,3 @@ def _async_setup_rpc_entry( |
89 | 167 | virtual_text_ids, |
90 | 168 | "enum", |
91 | 169 | ) |
92 | | - |
93 | | - |
94 | | -class RpcSelect(ShellyRpcAttributeEntity, SelectEntity): |
95 | | - """Represent a RPC select entity.""" |
96 | | - |
97 | | - entity_description: RpcSelectDescription |
98 | | - _id: int |
99 | | - |
100 | | - def __init__( |
101 | | - self, |
102 | | - coordinator: ShellyRpcCoordinator, |
103 | | - key: str, |
104 | | - attribute: str, |
105 | | - description: RpcSelectDescription, |
106 | | - ) -> None: |
107 | | - """Initialize select.""" |
108 | | - super().__init__(coordinator, key, attribute, description) |
109 | | - |
110 | | - self._attr_options = list(self.option_map.values()) |
111 | | - |
112 | | - @property |
113 | | - def current_option(self) -> str | None: |
114 | | - """Return the selected entity option to represent the entity state.""" |
115 | | - if not isinstance(self.attribute_value, str): |
116 | | - return None |
117 | | - |
118 | | - return self.option_map[self.attribute_value] |
119 | | - |
120 | | - @rpc_call |
121 | | - async def async_select_option(self, option: str) -> None: |
122 | | - """Change the value.""" |
123 | | - await self.coordinator.device.enum_set( |
124 | | - self._id, self.reversed_option_map[option] |
125 | | - ) |
|
0 commit comments