forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.py
192 lines (166 loc) · 6.22 KB
/
sensor.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""Sensor for myUplink."""
from myuplink.models import DevicePoint
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
UnitOfElectricCurrent,
UnitOfFrequency,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from . import MyUplinkDataCoordinator
from .const import DOMAIN
from .entity import MyUplinkEntity
DEVICE_POINT_UNIT_DESCRIPTIONS: dict[str, SensorEntityDescription] = {
"°C": SensorEntityDescription(
key="celsius",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
"A": SensorEntityDescription(
key="ampere",
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
),
"Hz": SensorEntityDescription(
key="hertz",
device_class=SensorDeviceClass.FREQUENCY,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfFrequency.HERTZ,
),
}
UNIQUE_PARAMETER_DESCRIPTIONS: dict[str, SensorEntityDescription] = {}
CATEGORY_BASED_DESCRIPTIONS: dict[str, dict[str, SensorEntityDescription]] = {
"NIBEF": {
"43108": SensorEntityDescription(
key="fan_mode",
icon="mdi:fan",
),
"43427": SensorEntityDescription(
key="status_compressor",
device_class=SensorDeviceClass.ENUM,
icon="mdi:heat-pump-outline",
),
"49993": SensorEntityDescription(
key="elect_add",
device_class=SensorDeviceClass.ENUM,
icon="mdi:heat-wave",
),
"49994": SensorEntityDescription(
key="priority",
device_class=SensorDeviceClass.ENUM,
icon="mdi:priority-high",
),
},
"NIBE": {},
}
def get_description(device_point: DevicePoint) -> SensorEntityDescription | None:
"""Get description for a device point.
Priorities:
1. Category specific prefix e.g "NIBEF"
2. Global parameter_id e.g. "12345"
3. Global parameter_unit e.g. "°C"
4. Default to None
"""
description = None
prefix, _, _ = device_point.category.partition(" ")
description = CATEGORY_BASED_DESCRIPTIONS.get(prefix, {}).get(
device_point.parameter_id
)
if description is None:
description = UNIQUE_PARAMETER_DESCRIPTIONS.get(device_point.parameter_id)
if description is None:
description = DEVICE_POINT_UNIT_DESCRIPTIONS.get(device_point.parameter_unit)
return description
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up myUplink sensor."""
entities: list[SensorEntity] = []
coordinator: MyUplinkDataCoordinator = hass.data[DOMAIN][config_entry.entry_id]
# Setup device point sensors
for device_id, point_data in coordinator.data.points.items():
for point_id, device_point in point_data.items():
description = get_description(device_point)
entity_class = MyUplinkDevicePointSensor
if (
description is not None
and description.device_class == SensorDeviceClass.ENUM
):
entity_class = MyUplinkEnumSensor
entities.append(
entity_class(
coordinator=coordinator,
device_id=device_id,
device_point=device_point,
entity_description=description,
unique_id_suffix=point_id,
)
)
async_add_entities(entities)
class MyUplinkDevicePointSensor(MyUplinkEntity, SensorEntity):
"""Representation of a myUplink device point sensor."""
def __init__(
self,
coordinator: MyUplinkDataCoordinator,
device_id: str,
device_point: DevicePoint,
entity_description: SensorEntityDescription | None,
unique_id_suffix: str,
) -> None:
"""Initialize the sensor."""
super().__init__(
coordinator=coordinator,
device_id=device_id,
unique_id_suffix=unique_id_suffix,
)
# Internal properties
self.point_id = device_point.parameter_id
self._attr_name = device_point.parameter_name
if entity_description is not None:
self.entity_description = entity_description
else:
self._attr_native_unit_of_measurement = device_point.parameter_unit
@property
def native_value(self) -> StateType:
"""Sensor state value."""
device_point = self.coordinator.data.points[self.device_id][self.point_id]
if device_point.value == -32768:
return None
return device_point.value # type: ignore[no-any-return]
class MyUplinkEnumSensor(MyUplinkDevicePointSensor):
"""Representation of a myUplink device point sensor for ENUM device_class."""
def __init__(
self,
coordinator: MyUplinkDataCoordinator,
device_id: str,
device_point: DevicePoint,
entity_description: SensorEntityDescription | None,
unique_id_suffix: str,
) -> None:
"""Initialize the sensor."""
super().__init__(
coordinator=coordinator,
device_id=device_id,
device_point=device_point,
entity_description=entity_description,
unique_id_suffix=unique_id_suffix,
)
self._attr_options = [x["text"].capitalize() for x in device_point.enum_values]
self.options_map = {x["value"]: x["text"] for x in device_point.enum_values}
@property
def native_value(self) -> str:
"""Sensor state value for enum sensor."""
device_point = self.coordinator.data.points[self.device_id][self.point_id]
return self.options_map[str(int(device_point.value))].capitalize() # type: ignore[no-any-return]