sensor.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. Setup for different kinds of Tuya sensors
  3. """
  4. import logging
  5. from homeassistant.components.sensor import (
  6. STATE_CLASSES,
  7. SensorDeviceClass,
  8. SensorEntity,
  9. )
  10. from .device import TuyaLocalDevice
  11. from .entity import TuyaLocalEntity, unit_from_ascii
  12. from .helpers.config import async_tuya_setup_platform
  13. from .helpers.device_config import TuyaEntityConfig
  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. super().__init__()
  34. dps_map = self._init_begin(device, config)
  35. self._sensor_dps = dps_map.pop("sensor", None)
  36. if self._sensor_dps is None:
  37. raise AttributeError(f"{config.config_id} is missing a sensor dps")
  38. self._unit_dps = dps_map.pop("unit", None)
  39. self._init_end(dps_map)
  40. @property
  41. def device_class(self):
  42. """Return the class of this device"""
  43. dclass = self._config.device_class
  44. if dclass:
  45. try:
  46. return SensorDeviceClass(dclass)
  47. except ValueError:
  48. _LOGGER.warning(
  49. "%s/%s: Unrecognized sensor device class of %s ignored",
  50. self._config._device.config,
  51. self.name or "sensor",
  52. dclass,
  53. )
  54. @property
  55. def state_class(self):
  56. """Return the state class of this entity"""
  57. sclass = self._sensor_dps.state_class
  58. if sclass in STATE_CLASSES:
  59. return sclass
  60. @property
  61. def native_value(self):
  62. """Return the value reported by the sensor"""
  63. return self._sensor_dps.get_value(self._device)
  64. @property
  65. def native_unit_of_measurement(self):
  66. """Return the unit for the sensor"""
  67. if self._unit_dps is None:
  68. unit = self._sensor_dps.unit
  69. else:
  70. unit = self._unit_dps.get_value(self._device)
  71. return unit_from_ascii(unit)
  72. @property
  73. def native_precision(self):
  74. """Return the precision for the sensor"""
  75. return self._sensor_dps.precision(self._device)
  76. @property
  77. def suggested_display_precision(self):
  78. """Return the suggested display precision for the sensor"""
  79. return self._sensor_dps.suggested_display_precision
  80. @property
  81. def options(self):
  82. """Return a set of possible options."""
  83. # if mappings are all integers, they are not options to HA
  84. values = self._sensor_dps.values(self._device)
  85. if values:
  86. for val in values:
  87. if isinstance(val, str):
  88. return values