switch.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. Platform to control the Open Window Detector on Purline M100 heaters.
  3. """
  4. from homeassistant.components.switch import SwitchEntity
  5. from homeassistant.components.switch import DEVICE_CLASS_SWITCH
  6. from homeassistant.const import STATE_UNAVAILABLE
  7. from .const import (
  8. ATTR_OPEN_WINDOW_DETECT,
  9. PROPERTY_TO_DPS_ID,
  10. )
  11. class PurlineM100OpenWindowDetector(SwitchEntity):
  12. """Representation of the Open Window Detection of a Purline M100 heater"""
  13. def __init__(self, device):
  14. """Initialize the switch.
  15. Args:
  16. device (TuyaLocalDevice): The device API instance."""
  17. self._device = device
  18. @property
  19. def should_poll(self):
  20. """Return the polling state."""
  21. return True
  22. @property
  23. def name(self):
  24. """Return the name of the switch."""
  25. return self._device.name
  26. @property
  27. def unique_id(self):
  28. """Return the unique id for this switch."""
  29. return self._device.unique_id
  30. @property
  31. def device_info(self):
  32. """Return device information about this switch."""
  33. return self._device.device_info
  34. @property
  35. def device_class(self):
  36. """Return the class of this device"""
  37. return DEVICE_CLASS_SWITCH
  38. @property
  39. def is_on(self):
  40. """Return the whether the switch is on."""
  41. is_switched_on = self._device.get_property(
  42. PROPERTY_TO_DPS_ID[ATTR_OPEN_WINDOW_DETECT]
  43. )
  44. if is_switched_on is None:
  45. return STATE_UNAVAILABLE
  46. else:
  47. return is_switched_on
  48. async def async_turn_on(self, **kwargs):
  49. """Turn the switch on"""
  50. await self._device.async_set_property(
  51. PROPERTY_TO_DPS_ID[ATTR_OPEN_WINDOW_DETECT], True
  52. )
  53. async def async_turn_off(self, **kwargs):
  54. """Turn the switch off"""
  55. await self._device.async_set_property(
  56. PROPERTY_TO_DPS_ID[ATTR_OPEN_WINDOW_DETECT], False
  57. )
  58. async def async_update(self):
  59. await self._device.async_refresh()