1 """Support for 1-Wire environment sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable, Mapping
9 from types
import MappingProxyType
10 from typing
import Any
12 from pyownet
import protocol
17 SensorEntityDescription,
23 UnitOfElectricPotential,
31 from .
import OneWireConfigEntry
35 OPTION_ENTRY_DEVICE_OPTIONS,
36 OPTION_ENTRY_SENSOR_PRECISION,
37 PRECISION_MAPPING_FAMILY_28,
41 from .entity
import OneWireEntity, OneWireEntityDescription
42 from .onewirehub
import OneWireHub
45 @dataclasses.dataclass(frozen=True)
47 """Class describing OneWire sensor entities."""
49 override_key: Callable[[str, Mapping[str, Any]], str] |
None =
None
53 """Get precision form config flow options."""
55 options.get(OPTION_ENTRY_DEVICE_OPTIONS, {})
57 .
get(OPTION_ENTRY_SENSOR_PRECISION,
"temperature")
59 if precision
in PRECISION_MAPPING_FAMILY_28:
62 "Invalid sensor precision `%s` for device `%s`: reverting to default",
71 device_class=SensorDeviceClass.TEMPERATURE,
72 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
73 read_mode=READ_MODE_FLOAT,
74 state_class=SensorStateClass.MEASUREMENT,
77 _LOGGER = logging.getLogger(__name__)
80 DEVICE_SENSORS: dict[str, tuple[OneWireSensorEntityDescription, ...]] = {
81 "10": (SIMPLE_TEMPERATURE_SENSOR_DESCRIPTION,),
84 key=
"TAI8570/temperature",
85 device_class=SensorDeviceClass.TEMPERATURE,
86 entity_registry_enabled_default=
False,
87 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
88 read_mode=READ_MODE_FLOAT,
89 state_class=SensorStateClass.MEASUREMENT,
92 key=
"TAI8570/pressure",
93 device_class=SensorDeviceClass.PRESSURE,
94 entity_registry_enabled_default=
False,
95 native_unit_of_measurement=UnitOfPressure.MBAR,
96 read_mode=READ_MODE_FLOAT,
97 state_class=SensorStateClass.MEASUREMENT,
100 "22": (SIMPLE_TEMPERATURE_SENSOR_DESCRIPTION,),
102 SIMPLE_TEMPERATURE_SENSOR_DESCRIPTION,
105 device_class=SensorDeviceClass.HUMIDITY,
106 entity_registry_enabled_default=
False,
107 native_unit_of_measurement=PERCENTAGE,
108 read_mode=READ_MODE_FLOAT,
109 state_class=SensorStateClass.MEASUREMENT,
112 key=
"HIH3600/humidity",
113 device_class=SensorDeviceClass.HUMIDITY,
114 entity_registry_enabled_default=
False,
115 native_unit_of_measurement=PERCENTAGE,
116 read_mode=READ_MODE_FLOAT,
117 state_class=SensorStateClass.MEASUREMENT,
118 translation_key=
"humidity_hih3600",
121 key=
"HIH4000/humidity",
122 device_class=SensorDeviceClass.HUMIDITY,
123 entity_registry_enabled_default=
False,
124 native_unit_of_measurement=PERCENTAGE,
125 read_mode=READ_MODE_FLOAT,
126 state_class=SensorStateClass.MEASUREMENT,
127 translation_key=
"humidity_hih4000",
130 key=
"HIH5030/humidity",
131 device_class=SensorDeviceClass.HUMIDITY,
132 entity_registry_enabled_default=
False,
133 native_unit_of_measurement=PERCENTAGE,
134 read_mode=READ_MODE_FLOAT,
135 state_class=SensorStateClass.MEASUREMENT,
136 translation_key=
"humidity_hih5030",
139 key=
"HTM1735/humidity",
140 device_class=SensorDeviceClass.HUMIDITY,
141 entity_registry_enabled_default=
False,
142 native_unit_of_measurement=PERCENTAGE,
143 read_mode=READ_MODE_FLOAT,
144 state_class=SensorStateClass.MEASUREMENT,
145 translation_key=
"humidity_htm1735",
148 key=
"B1-R1-A/pressure",
149 device_class=SensorDeviceClass.PRESSURE,
150 entity_registry_enabled_default=
False,
151 native_unit_of_measurement=UnitOfPressure.MBAR,
152 read_mode=READ_MODE_FLOAT,
153 state_class=SensorStateClass.MEASUREMENT,
156 key=
"S3-R1-A/illuminance",
157 device_class=SensorDeviceClass.ILLUMINANCE,
158 entity_registry_enabled_default=
False,
159 native_unit_of_measurement=LIGHT_LUX,
160 read_mode=READ_MODE_FLOAT,
161 state_class=SensorStateClass.MEASUREMENT,
165 device_class=SensorDeviceClass.VOLTAGE,
166 entity_registry_enabled_default=
False,
167 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
168 read_mode=READ_MODE_FLOAT,
169 state_class=SensorStateClass.MEASUREMENT,
170 translation_key=
"voltage_vad",
174 device_class=SensorDeviceClass.VOLTAGE,
175 entity_registry_enabled_default=
False,
176 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
177 read_mode=READ_MODE_FLOAT,
178 state_class=SensorStateClass.MEASUREMENT,
179 translation_key=
"voltage_vdd",
183 device_class=SensorDeviceClass.VOLTAGE,
184 entity_registry_enabled_default=
False,
185 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
186 read_mode=READ_MODE_FLOAT,
187 state_class=SensorStateClass.MEASUREMENT,
188 translation_key=
"voltage_vis",
194 device_class=SensorDeviceClass.TEMPERATURE,
195 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
196 override_key=_get_sensor_precision_family_28,
197 read_mode=READ_MODE_FLOAT,
198 state_class=SensorStateClass.MEASUREMENT,
202 SIMPLE_TEMPERATURE_SENSOR_DESCRIPTION,
204 key=
"typeX/temperature",
205 device_class=SensorDeviceClass.TEMPERATURE,
206 entity_registry_enabled_default=
False,
207 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
208 read_mode=READ_MODE_FLOAT,
209 override_key=
lambda d, o:
"typeK/temperature",
210 state_class=SensorStateClass.MEASUREMENT,
211 translation_key=
"thermocouple_temperature_k",
215 device_class=SensorDeviceClass.VOLTAGE,
216 entity_registry_enabled_default=
False,
217 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
218 read_mode=READ_MODE_FLOAT,
219 state_class=SensorStateClass.MEASUREMENT,
223 device_class=SensorDeviceClass.VOLTAGE,
224 entity_registry_enabled_default=
False,
225 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
226 read_mode=READ_MODE_FLOAT,
227 state_class=SensorStateClass.MEASUREMENT,
228 translation_key=
"voltage_vis_gradient",
231 "3B": (SIMPLE_TEMPERATURE_SENSOR_DESCRIPTION,),
232 "42": (SIMPLE_TEMPERATURE_SENSOR_DESCRIPTION,),
235 key=f
"counter.{device_key}",
236 native_unit_of_measurement=
"count",
237 read_mode=READ_MODE_INT,
238 state_class=SensorStateClass.TOTAL_INCREASING,
239 translation_key=
"counter_id",
240 translation_placeholders={
"id":
str(device_key)},
242 for device_key
in DEVICE_KEYS_A_B
248 HOBBYBOARD_EF: dict[str, tuple[OneWireSensorEntityDescription, ...]] = {
251 key=
"humidity/humidity_corrected",
252 device_class=SensorDeviceClass.HUMIDITY,
253 native_unit_of_measurement=PERCENTAGE,
254 read_mode=READ_MODE_FLOAT,
255 state_class=SensorStateClass.MEASUREMENT,
258 key=
"humidity/humidity_raw",
259 device_class=SensorDeviceClass.HUMIDITY,
260 native_unit_of_measurement=PERCENTAGE,
261 read_mode=READ_MODE_FLOAT,
262 state_class=SensorStateClass.MEASUREMENT,
263 translation_key=
"humidity_raw",
266 key=
"humidity/temperature",
267 device_class=SensorDeviceClass.TEMPERATURE,
268 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
269 read_mode=READ_MODE_FLOAT,
270 state_class=SensorStateClass.MEASUREMENT,
273 "HB_MOISTURE_METER":
tuple(
275 key=f
"moisture/sensor.{device_key}",
276 device_class=SensorDeviceClass.PRESSURE,
277 native_unit_of_measurement=UnitOfPressure.CBAR,
278 read_mode=READ_MODE_FLOAT,
279 state_class=SensorStateClass.MEASUREMENT,
280 translation_key=
"moisture_id",
281 translation_placeholders={
"id":
str(device_key)},
283 for device_key
in DEVICE_KEYS_0_3
289 EDS_SENSORS: dict[str, tuple[OneWireSensorEntityDescription, ...]] = {
292 key=
"EDS0066/temperature",
293 device_class=SensorDeviceClass.TEMPERATURE,
294 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
295 read_mode=READ_MODE_FLOAT,
296 state_class=SensorStateClass.MEASUREMENT,
299 key=
"EDS0066/pressure",
300 device_class=SensorDeviceClass.PRESSURE,
301 native_unit_of_measurement=UnitOfPressure.MBAR,
302 read_mode=READ_MODE_FLOAT,
303 state_class=SensorStateClass.MEASUREMENT,
308 key=
"EDS0068/temperature",
309 device_class=SensorDeviceClass.TEMPERATURE,
310 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
311 read_mode=READ_MODE_FLOAT,
312 state_class=SensorStateClass.MEASUREMENT,
315 key=
"EDS0068/pressure",
316 device_class=SensorDeviceClass.PRESSURE,
317 native_unit_of_measurement=UnitOfPressure.MBAR,
318 read_mode=READ_MODE_FLOAT,
319 state_class=SensorStateClass.MEASUREMENT,
323 device_class=SensorDeviceClass.ILLUMINANCE,
324 native_unit_of_measurement=LIGHT_LUX,
325 read_mode=READ_MODE_FLOAT,
326 state_class=SensorStateClass.MEASUREMENT,
329 key=
"EDS0068/humidity",
330 device_class=SensorDeviceClass.HUMIDITY,
331 native_unit_of_measurement=PERCENTAGE,
332 read_mode=READ_MODE_FLOAT,
333 state_class=SensorStateClass.MEASUREMENT,
340 device_sub_type: str,
341 ) -> dict[str, tuple[OneWireSensorEntityDescription, ...]]:
342 """Return the proper info array for the device type."""
343 if "HobbyBoard" in device_sub_type:
345 if "EDS" in device_sub_type:
347 return DEVICE_SENSORS
352 config_entry: OneWireConfigEntry,
353 async_add_entities: AddEntitiesCallback,
355 """Set up 1-Wire platform."""
356 entities = await hass.async_add_executor_job(
357 get_entities, config_entry.runtime_data, config_entry.options
363 onewire_hub: OneWireHub, options: MappingProxyType[str, Any]
364 ) -> list[OneWireSensor]:
365 """Get a list of entities."""
366 if not onewire_hub.devices:
369 entities: list[OneWireSensor] = []
370 assert onewire_hub.owproxy
371 for device
in onewire_hub.devices:
372 family = device.family
373 device_type = device.type
374 device_id = device.id
375 device_info = device.device_info
376 device_sub_type =
"std"
377 device_path = device.path
378 if device_type
and "EF" in family:
379 device_sub_type =
"HobbyBoard"
381 elif device_type
and "7E" in family:
382 device_sub_type =
"EDS"
391 if description.key.startswith(
"moisture/"):
392 s_id = description.key.split(
".")[1]
394 onewire_hub.owproxy.read(
395 f
"{device_path}moisture/is_leaf.{s_id}"
399 description = dataclasses.replace(
401 device_class=SensorDeviceClass.HUMIDITY,
402 native_unit_of_measurement=PERCENTAGE,
403 translation_key=
"wetness_id",
404 translation_placeholders={
"id": s_id},
407 if description.override_key:
408 override_key = description.override_key(device_id, options)
409 device_file = os.path.join(
410 os.path.split(device.path)[0],
411 override_key
or description.key,
416 onewire_hub.owproxy.read(device_file)
417 except protocol.OwnetError
as err:
419 "Ignoring unreachable sensor %s",
426 description=description,
428 device_file=device_file,
429 device_info=device_info,
430 owproxy=onewire_hub.owproxy,
437 """Implementation of a 1-Wire sensor."""
439 entity_description: OneWireSensorEntityDescription
443 """Return the state of the entity."""
StateType native_value(self)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, OneWireConfigEntry config_entry, AddEntitiesCallback async_add_entities)
str _get_sensor_precision_family_28(str device_id, Mapping[str, Any] options)
list[OneWireSensor] get_entities(OneWireHub onewire_hub, MappingProxyType[str, Any] options)
dict[str, tuple[OneWireSensorEntityDescription,...]] get_sensor_types(str device_sub_type)