binary_sensor.py 1.3 KB

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