binary_sensor.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. Setup for different kinds of Tuya Binary sensors
  3. """
  4. import logging
  5. from homeassistant.components.binary_sensor import (
  6. BinarySensorDeviceClass,
  7. BinarySensorEntity,
  8. )
  9. from .device import TuyaLocalDevice
  10. from .entity import TuyaLocalEntity
  11. from .helpers.config import async_tuya_setup_platform
  12. from .helpers.device_config import TuyaEntityConfig
  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. super().__init__()
  33. dps_map = self._init_begin(device, config)
  34. self._sensor_dps = dps_map.pop("sensor")
  35. if self._sensor_dps is None:
  36. raise AttributeError(f"{config.config_id} is missing a sensor dps")
  37. self._init_end(dps_map)
  38. @property
  39. def device_class(self):
  40. """Return the class of this device"""
  41. dclass = self._config.device_class
  42. try:
  43. return BinarySensorDeviceClass(dclass)
  44. except ValueError:
  45. if dclass:
  46. _LOGGER.warning(
  47. "%s/%s: Unrecognised binary_sensor device class of %s ignored",
  48. self._config._device.config,
  49. self.name or "binary_sensor",
  50. dclass,
  51. )
  52. @property
  53. def is_on(self):
  54. """Return true if the binary sensor is on."""
  55. return self._sensor_dps.get_value(self._device)