sensor.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """
  2. Setup for different kinds of Tuya sensors
  3. """
  4. from homeassistant.components.sensor import (
  5. SensorDeviceClass,
  6. SensorEntity,
  7. STATE_CLASSES,
  8. )
  9. import logging
  10. from .device import TuyaLocalDevice
  11. from .helpers.config import async_tuya_setup_platform
  12. from .helpers.device_config import TuyaEntityConfig
  13. from .helpers.mixin import TuyaLocalEntity, unit_from_ascii
  14. _LOGGER = logging.getLogger(__name__)
  15. async def async_setup_entry(hass, config_entry, async_add_entities):
  16. config = {**config_entry.data, **config_entry.options}
  17. await async_tuya_setup_platform(
  18. hass,
  19. async_add_entities,
  20. config,
  21. "sensor",
  22. TuyaLocalSensor,
  23. )
  24. class TuyaLocalSensor(TuyaLocalEntity, SensorEntity):
  25. """Representation of a Tuya Sensor"""
  26. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  27. """
  28. Initialise the sensor.
  29. Args:
  30. device (TuyaLocalDevice): the device API instance.
  31. config (TuyaEntityConfig): the configuration for this entity
  32. """
  33. dps_map = self._init_begin(device, config)
  34. self._sensor_dps = dps_map.pop("sensor", None)
  35. if self._sensor_dps is None:
  36. raise AttributeError(f"{config.name} is missing a sensor dps")
  37. self._unit_dps = dps_map.pop("unit", None)
  38. self._init_end(dps_map)
  39. @property
  40. def device_class(self):
  41. """Return the class of this device"""
  42. dclass = self._config.device_class
  43. if dclass:
  44. try:
  45. return SensorDeviceClass(dclass)
  46. except ValueError:
  47. _LOGGER.warning(
  48. "Unrecognized sensor device class of %s ignored", dclass
  49. )
  50. @property
  51. def state_class(self):
  52. """Return the state class of this entity"""
  53. sclass = self._sensor_dps.state_class
  54. if sclass in STATE_CLASSES:
  55. return sclass
  56. else:
  57. return None
  58. @property
  59. def native_value(self):
  60. """Return the value reported by the sensor"""
  61. return self._sensor_dps.get_value(self._device)
  62. @property
  63. def native_unit_of_measurement(self):
  64. """Return the unit for the sensor"""
  65. if self._unit_dps is None:
  66. unit = self._sensor_dps.unit
  67. else:
  68. unit = self._unit_dps.get_value(self._device)
  69. return unit_from_ascii(unit)
  70. @property
  71. def native_precision(self):
  72. """Return the precision for the sensor"""
  73. return self._sensor_dps.precision(self._device)
  74. @property
  75. def options(self):
  76. """Return a set of possible options."""
  77. # if mappings are all integers, they are not options to HA
  78. values = self._sensor_dps.values(self._device)
  79. if values:
  80. for val in values:
  81. if isinstance(val, str):
  82. return values