binary_sensor.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. Platform to read Tuya binary sensors.
  3. """
  4. from homeassistant.components.binary_sensor import (
  5. BinarySensorEntity,
  6. BinarySensorDeviceClass,
  7. )
  8. import logging
  9. from ..device import TuyaLocalDevice
  10. from ..helpers.device_config import TuyaEntityConfig
  11. from ..helpers.mixin import TuyaLocalEntity
  12. _LOGGER = logging.getLogger(__name__)
  13. class TuyaLocalBinarySensor(TuyaLocalEntity, BinarySensorEntity):
  14. """Representation of a Tuya Binary Sensor"""
  15. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  16. """
  17. Initialise the sensor.
  18. Args:
  19. device (TuyaLocalDevice): the device API instance.
  20. config (TuyaEntityConfig): the configuration for this entity
  21. """
  22. dps_map = self._init_begin(device, config)
  23. self._sensor_dps = dps_map.pop("sensor")
  24. if self._sensor_dps is None:
  25. raise AttributeError(f"{config.name} is missing a sensor dps")
  26. self._init_end(dps_map)
  27. @property
  28. def device_class(self):
  29. """Return the class of this device"""
  30. dclass = self._config.device_class
  31. try:
  32. return BinarySensorDeviceClass(dclass)
  33. except ValueError:
  34. if dclass:
  35. _LOGGER.warning(
  36. f"Unrecognised binary_sensor device class of {dclass} ignored"
  37. )
  38. return None
  39. @property
  40. def is_on(self):
  41. """Return true if the binary sensor is on."""
  42. return self._sensor_dps.get_value(self._device)