sensor.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 .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. 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.name} 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. "Unrecognized sensor device class of %s ignored", dclass
  50. )
  51. @property
  52. def state_class(self):
  53. """Return the state class of this entity"""
  54. sclass = self._sensor_dps.state_class
  55. if sclass in STATE_CLASSES:
  56. return sclass
  57. else:
  58. return None
  59. @property
  60. def native_value(self):
  61. """Return the value reported by the sensor"""
  62. return self._sensor_dps.get_value(self._device)
  63. @property
  64. def native_unit_of_measurement(self):
  65. """Return the unit for the sensor"""
  66. if self._unit_dps is None:
  67. unit = self._sensor_dps.unit
  68. else:
  69. unit = self._unit_dps.get_value(self._device)
  70. return unit_from_ascii(unit)
  71. @property
  72. def native_precision(self):
  73. """Return the precision for the sensor"""
  74. return self._sensor_dps.precision(self._device)
  75. @property
  76. def suggested_display_precision(self):
  77. """Return the suggested display precision for the sensor"""
  78. return self._sensor_dps.suggested_display_precision
  79. @property
  80. def options(self):
  81. """Return a set of possible options."""
  82. # if mappings are all integers, they are not options to HA
  83. values = self._sensor_dps.values(self._device)
  84. if values:
  85. for val in values:
  86. if isinstance(val, str):
  87. return values