binary_sensor.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. Platform to sense whether the dehumidifier tank is full.
  3. """
  4. from homeassistant.components.binary_sensor import (BinarySensorDevice, DEVICE_CLASS_PROBLEM)
  5. from custom_components.tuya_local import TuyaLocalDevice
  6. from custom_components.tuya_local.dehumidifier.climate import (
  7. ATTR_FAULT, FAULT_CODE_TO_DPS_CODE, PROPERTY_TO_DPS_ID
  8. )
  9. ATTR_FAULT_CODE = 'fault_code'
  10. FAULT_TANK = 8
  11. FAULT_NONE = 0
  12. class GoldairDehumidifierTankFullBinarySensor(BinarySensorDevice):
  13. """Representation of a Goldair WiFi-connected dehumidifier Tank sensor."""
  14. def __init__(self, device):
  15. """Initialize the binary sensor.
  16. Args:
  17. device (TuyaLocalDevice): The device API instance."""
  18. self._device = device
  19. self._fault = None
  20. @property
  21. def should_poll(self):
  22. """Return the polling state"""
  23. return True
  24. @property
  25. def name(self):
  26. """Return the name of the binary sensor."""
  27. return self._device.name
  28. @property
  29. def is_on(self):
  30. """Return true if the tank is full."""
  31. if (self._fault is None):
  32. return None
  33. else:
  34. return self._fault == FAULT_TANK
  35. @property
  36. def device_class(self):
  37. """Return the class of device."""
  38. return DEVICE_CLASS_PROBLEM
  39. @property
  40. def device_state_attributes(self):
  41. """Return the state attributes"""
  42. attrs = {ATTR_FAULT_CODE: self._fault}
  43. # attrs.update(super().device_state_attributes)
  44. return attrs
  45. @property
  46. def available(self):
  47. """Return true if the device is available and value has not expired"""
  48. return self._fault is not None
  49. def update(self):
  50. self._device.refresh()
  51. self._fault = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_FAULT])