sensor.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 suggested_display_precision(self):
  76. """Return the suggested display precision for the sensor"""
  77. return self._sensor_dps.suggested_display_precision
  78. @property
  79. def options(self):
  80. """Return a set of possible options."""
  81. # if mappings are all integers, they are not options to HA
  82. values = self._sensor_dps.values(self._device)
  83. if values:
  84. for val in values:
  85. if isinstance(val, str):
  86. return values