Add device_class information to onewire sensors (#42308)
This commit is contained in:
parent
d13402108b
commit
5db1474099
3 changed files with 87 additions and 33 deletions
|
@ -2,6 +2,7 @@
|
|||
from glob import glob
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from pi1wire import InvalidCRCException, Pi1Wire, UnsupportResponseException
|
||||
from pyownet import protocol
|
||||
|
@ -13,6 +14,12 @@ from homeassistant.const import (
|
|||
CONF_HOST,
|
||||
CONF_PORT,
|
||||
CONF_TYPE,
|
||||
DEVICE_CLASS_CURRENT,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_ILLUMINANCE,
|
||||
DEVICE_CLASS_PRESSURE,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
DEVICE_CLASS_VOLTAGE,
|
||||
ELECTRICAL_CURRENT_AMPERE,
|
||||
LIGHT_LUX,
|
||||
PERCENTAGE,
|
||||
|
@ -22,6 +29,7 @@ from homeassistant.const import (
|
|||
)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from .const import (
|
||||
CONF_MOUNT_DIR,
|
||||
|
@ -83,31 +91,31 @@ HOBBYBOARD_EF = {
|
|||
}
|
||||
|
||||
SENSOR_TYPES = {
|
||||
# SensorType: [ Measured unit, Unit ]
|
||||
"temperature": ["temperature", TEMP_CELSIUS],
|
||||
"humidity": ["humidity", PERCENTAGE],
|
||||
"humidity_hih3600": ["humidity", PERCENTAGE],
|
||||
"humidity_hih4000": ["humidity", PERCENTAGE],
|
||||
"humidity_hih5030": ["humidity", PERCENTAGE],
|
||||
"humidity_htm1735": ["humidity", PERCENTAGE],
|
||||
"humidity_raw": ["humidity", PERCENTAGE],
|
||||
"pressure": ["pressure", PRESSURE_MBAR],
|
||||
"illuminance": ["illuminance", LIGHT_LUX],
|
||||
"wetness_0": ["wetness", PERCENTAGE],
|
||||
"wetness_1": ["wetness", PERCENTAGE],
|
||||
"wetness_2": ["wetness", PERCENTAGE],
|
||||
"wetness_3": ["wetness", PERCENTAGE],
|
||||
"moisture_0": ["moisture", PRESSURE_CBAR],
|
||||
"moisture_1": ["moisture", PRESSURE_CBAR],
|
||||
"moisture_2": ["moisture", PRESSURE_CBAR],
|
||||
"moisture_3": ["moisture", PRESSURE_CBAR],
|
||||
"counter_a": ["counter", "count"],
|
||||
"counter_b": ["counter", "count"],
|
||||
"HobbyBoard": ["none", "none"],
|
||||
"voltage": ["voltage", VOLT],
|
||||
"voltage_VAD": ["voltage", VOLT],
|
||||
"voltage_VDD": ["voltage", VOLT],
|
||||
"current": ["current", ELECTRICAL_CURRENT_AMPERE],
|
||||
# SensorType: [ Measured unit, Unit, DeviceClass ]
|
||||
"temperature": ["temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
|
||||
"humidity": ["humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
"humidity_hih3600": ["humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
"humidity_hih4000": ["humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
"humidity_hih5030": ["humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
"humidity_htm1735": ["humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
"humidity_raw": ["humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
"pressure": ["pressure", PRESSURE_MBAR, DEVICE_CLASS_PRESSURE],
|
||||
"illuminance": ["illuminance", LIGHT_LUX, DEVICE_CLASS_ILLUMINANCE],
|
||||
"wetness_0": ["wetness", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
"wetness_1": ["wetness", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
"wetness_2": ["wetness", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
"wetness_3": ["wetness", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
"moisture_0": ["moisture", PRESSURE_CBAR, DEVICE_CLASS_PRESSURE],
|
||||
"moisture_1": ["moisture", PRESSURE_CBAR, DEVICE_CLASS_PRESSURE],
|
||||
"moisture_2": ["moisture", PRESSURE_CBAR, DEVICE_CLASS_PRESSURE],
|
||||
"moisture_3": ["moisture", PRESSURE_CBAR, DEVICE_CLASS_PRESSURE],
|
||||
"counter_a": ["counter", "count", None],
|
||||
"counter_b": ["counter", "count", None],
|
||||
"HobbyBoard": ["none", "none", None],
|
||||
"voltage": ["voltage", VOLT, DEVICE_CLASS_VOLTAGE],
|
||||
"voltage_VAD": ["voltage", VOLT, DEVICE_CLASS_VOLTAGE],
|
||||
"voltage_VDD": ["voltage", VOLT, DEVICE_CLASS_VOLTAGE],
|
||||
"current": ["current", ELECTRICAL_CURRENT_AMPERE, DEVICE_CLASS_CURRENT],
|
||||
}
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
|
@ -282,34 +290,40 @@ class OneWire(Entity):
|
|||
"""Initialize the sensor."""
|
||||
self._name = f"{name} {sensor_type.capitalize()}"
|
||||
self._device_file = device_file
|
||||
self._device_class = SENSOR_TYPES[sensor_type][2]
|
||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
||||
self._state = None
|
||||
self._value_raw = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
def name(self) -> Optional[str]:
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
def state(self) -> StateType:
|
||||
"""Return the state of the sensor."""
|
||||
if "count" in self._unit_of_measurement:
|
||||
return int(self._state)
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
def device_class(self) -> Optional[str]:
|
||||
"""Return the class of this device."""
|
||||
return self._device_class
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> Optional[str]:
|
||||
"""Return the unit the value is expressed in."""
|
||||
return self._unit_of_measurement
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
def device_state_attributes(self) -> Optional[Dict[str, Any]]:
|
||||
"""Return the state attributes of the sensor."""
|
||||
return {"device_file": self._device_file, "raw_value": self._value_raw}
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
def unique_id(self) -> Optional[str]:
|
||||
"""Return a unique ID."""
|
||||
return self._device_file
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue