binary_sensor.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """
  2. Setup for different kinds of 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.config import async_tuya_setup_platform
  11. from .helpers.device_config import TuyaEntityConfig
  12. from .helpers.mixin import TuyaLocalEntity
  13. _LOGGER = logging.getLogger(__name__)
  14. async def async_setup_entry(hass, config_entry, async_add_entities):
  15. config = {**config_entry.data, **config_entry.options}
  16. await async_tuya_setup_platform(
  17. hass,
  18. async_add_entities,
  19. config,
  20. "binary_sensor",
  21. TuyaLocalBinarySensor,
  22. )
  23. class TuyaLocalBinarySensor(TuyaLocalEntity, BinarySensorEntity):
  24. """Representation of a Tuya Binary Sensor"""
  25. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  26. """
  27. Initialise the sensor.
  28. Args:
  29. device (TuyaLocalDevice): the device API instance.
  30. config (TuyaEntityConfig): the configuration for this entity
  31. """
  32. dps_map = self._init_begin(device, config)
  33. self._sensor_dps = dps_map.pop("sensor")
  34. if self._sensor_dps is None:
  35. raise AttributeError(f"{config.name} is missing a sensor dps")
  36. self._init_end(dps_map)
  37. @property
  38. def device_class(self):
  39. """Return the class of this device"""
  40. dclass = self._config.device_class
  41. try:
  42. return BinarySensorDeviceClass(dclass)
  43. except ValueError:
  44. if dclass:
  45. _LOGGER.warning(
  46. f"Unrecognised binary_sensor device class of {dclass} ignored"
  47. )
  48. return None
  49. @property
  50. def is_on(self):
  51. """Return true if the binary sensor is on."""
  52. return self._sensor_dps.get_value(self._device)