Files
localtuya-modded/custom_components/localtuya/sensor.py
2020-09-26 22:38:26 +02:00

91 lines
2.4 KiB
Python

"""Platform to present any Tuya DP as a sensor."""
import logging
import voluptuous as vol
from homeassistant.components.sensor import DOMAIN, DEVICE_CLASSES
from homeassistant.const import (
CONF_ID,
CONF_DEVICE_CLASS,
CONF_UNIT_OF_MEASUREMENT,
STATE_UNKNOWN,
)
from .const import CONF_SCALING
from .common import LocalTuyaEntity, prepare_setup_entities
_LOGGER = logging.getLogger(__name__)
DEFAULT_SCALING = 1.0
DEFAULT_PRECISION = 2
def flow_schema(dps):
"""Return schema used in config flow."""
return {
vol.Optional(CONF_UNIT_OF_MEASUREMENT): str,
vol.Optional(CONF_DEVICE_CLASS): vol.In(DEVICE_CLASSES),
vol.Optional(CONF_SCALING, default=DEFAULT_SCALING): vol.All(
vol.Coerce(float), vol.Range(min=-1000000.0, max=1000000.0)
),
}
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up a Tuya sensor based on a config entry."""
tuyainterface, entities_to_setup = prepare_setup_entities(
hass, config_entry, DOMAIN
)
if not entities_to_setup:
return
sensors = []
for device_config in entities_to_setup:
sensors.append(
LocaltuyaSensor(
tuyainterface,
config_entry,
device_config[CONF_ID],
)
)
async_add_entities(sensors, True)
class LocaltuyaSensor(LocalTuyaEntity):
"""Representation of a Tuya sensor."""
def __init__(
self,
device,
config_entry,
sensorid,
**kwargs,
):
"""Initialize the Tuya sensor."""
super().__init__(device, config_entry, sensorid, **kwargs)
self._state = STATE_UNKNOWN
@property
def state(self):
"""Return sensor state."""
return self._state
@property
def device_class(self):
"""Return the class of this device."""
return self._config.get(CONF_DEVICE_CLASS)
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self._config.get(CONF_UNIT_OF_MEASUREMENT)
def status_updated(self):
"""Device status was updated."""
state = self.dps(self._dps_id)
scale_factor = self._config.get(CONF_SCALING)
if scale_factor is not None:
state = round(state * scale_factor, DEFAULT_PRECISION)
self._state = state