Repository navigation
Expand file tree
/
Copy pathselect.py
More file actions
176 lines (153 loc) · 6.89 KB
/
Copy pathselect.py
File metadata and controls
176 lines (153 loc) · 6.89 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""Select platform - map holding-register values to friendly options."""
from __future__ import annotations
from typing import Any, Optional
import logging
from homeassistant.components.select import SelectEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.exceptions import HomeAssistantError
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import DeyeModbusCoordinator
from .device_helper import build_device_info
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None:
data = hass.data[DOMAIN][entry.entry_id]
coord: DeyeModbusCoordinator = data["coordinator"]
entities = []
for c in data.get("controls", []):
if c.get("type") == "select":
entities.append(DeyeModbusSelect(coord, entry, c))
elif c.get("type") == "select32":
entities.append(DeyeModbusSelect32(coord, entry, c))
_LOGGER.info("Adding %s Deye select entities", len(entities))
if entities:
async_add_entities(entities)
def _apply_entity_category(entity, cfg):
ec = cfg.get("entity_category")
if ec:
try:
entity._attr_entity_category = EntityCategory(ec)
except ValueError:
_LOGGER.warning("Unknown entity_category %r", ec)
class DeyeModbusSelect(CoordinatorEntity[dict[str, Any]], SelectEntity):
_attr_has_entity_name = True
def __init__(self, coordinator: DeyeModbusCoordinator, entry: ConfigEntry, cfg: dict[str, Any]) -> None:
super().__init__(coordinator)
self._coordinator = coordinator
self._entry = entry
self._cfg = cfg
self._address = int(cfg["address"])
opts = cfg.get("options", [])
self._labels = [o["label"] for o in opts]
self._value_by_label = {o["label"]: int(o["value"]) for o in opts}
self._label_by_value = {int(o["value"]): o["label"] for o in opts}
self._attr_name = cfg.get("name")
uid = cfg.get("unique_id") or f"select_{self._address}"
self._attr_unique_id = f"{entry.entry_id}_{uid}"
self._attr_device_info = build_device_info(entry)
self._attr_options = self._labels
self._attr_entity_registry_enabled_default = bool(cfg.get("enabled_default", True))
if cfg.get("icon"):
self._attr_icon = cfg["icon"]
_apply_entity_category(self, cfg)
self._current_option: Optional[str] = None
self._read_uid: Optional[str] = cfg.get("read_unique_id")
self._read_factor: float = float(cfg.get("read_factor", 1.0))
self._sync_from_sensor()
@property
def current_option(self) -> str | None:
return self._current_option
async def async_select_option(self, option: str) -> None:
if option not in self._value_by_label:
return
ok = await self._coordinator.write_single_register(self._address, self._value_by_label[option])
if not ok:
raise HomeAssistantError(
f"Writing {self._attr_name} to the inverter failed (register {self._address}); see log"
)
if ok:
self._current_option = option
self.async_write_ha_state()
await self._coordinator.async_request_refresh()
def _sync_from_sensor(self) -> None:
if not self._read_uid:
return
raw = (self._coordinator.data or {}).get(self._read_uid)
if raw is None:
return
try:
v = int(round(float(raw) / self._read_factor))
self._current_option = self._label_by_value.get(v, None)
except Exception: # noqa: BLE001
pass
def _handle_coordinator_update(self) -> None:
self._sync_from_sensor()
self.async_write_ha_state()
class DeyeModbusSelect32(CoordinatorEntity[dict[str, Any]], SelectEntity):
"""Select that edits only a bitfield inside a packed 32-bit register."""
_attr_has_entity_name = True
def __init__(self, coordinator: DeyeModbusCoordinator, entry: ConfigEntry, cfg: dict[str, Any]) -> None:
super().__init__(coordinator)
self._coordinator = coordinator
self._entry = entry
self._cfg = cfg
self._base = int(cfg["base_address"])
self._order = cfg.get("word_order", "high_low")
self._mask = int(str(cfg.get("mask")), 0) if "mask" in cfg else 0xFFFFFFFF
self._shift = int(cfg.get("shift", 0))
opts = cfg.get("options", [])
self._labels = [o["label"] for o in opts]
self._value_by_label = {o["label"]: int(o["value"]) for o in opts}
self._label_by_value = {int(o["value"]): o["label"] for o in opts}
self._attr_name = cfg.get("name")
uid = cfg.get("unique_id") or f"select32_{self._base}"
self._attr_unique_id = f"{entry.entry_id}_{uid}"
self._attr_device_info = build_device_info(entry)
self._attr_options = self._labels
self._attr_entity_registry_enabled_default = bool(cfg.get("enabled_default", True))
if cfg.get("icon"):
self._attr_icon = cfg["icon"]
_apply_entity_category(self, cfg)
self._current_option: Optional[str] = None
self._read_uid: Optional[str] = cfg.get("read_unique_id")
self._sync_from_sensor()
@property
def current_option(self) -> str | None:
return self._current_option
def _get_u32(self) -> Optional[int]:
if not self._read_uid:
return None
raw = (self._coordinator.data or {}).get(self._read_uid)
if raw is None:
return None
try:
return int(raw)
except Exception: # noqa: BLE001
return None
def _recompute_option_from_u32(self, v: int) -> None:
field = (v & self._mask) >> self._shift
self._current_option = self._label_by_value.get(field, None)
async def async_select_option(self, option: str) -> None:
if option not in self._value_by_label:
return
old = self._get_u32()
if old is None:
return
field_val = self._value_by_label[option] & (self._mask >> self._shift)
new = (old & (~self._mask)) | ((field_val << self._shift) & self._mask)
ok = await self._coordinator.write_u32(self._base, new, self._order)
if ok:
self._recompute_option_from_u32(new)
self.async_write_ha_state()
await self._coordinator.async_request_refresh()
def _sync_from_sensor(self) -> None:
v = self._get_u32()
if v is None:
return
self._recompute_option_from_u32(v)
def _handle_coordinator_update(self) -> None:
self._sync_from_sensor()
self.async_write_ha_state()