switch.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """
  2. Platform to control Tuya switches.
  3. Initially based on the Kogan Switch and secondary switch for Purline M100
  4. heater open window detector toggle.
  5. """
  6. from homeassistant.components.switch import SwitchEntity
  7. from homeassistant.components.switch import (
  8. ATTR_CURRENT_POWER_W,
  9. DEVICE_CLASS_OUTLET,
  10. DEVICE_CLASS_SWITCH,
  11. )
  12. from homeassistant.const import STATE_UNAVAILABLE
  13. from ..device import TuyaLocalDevice
  14. from ..helpers.device_config import TuyaEntityConfig
  15. class TuyaLocalSwitch(SwitchEntity):
  16. """Representation of a Tuya Switch"""
  17. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  18. """
  19. Initialize the switch.
  20. Args:
  21. device (TuyaLocalDevice): The device API instance.
  22. """
  23. self._device = device
  24. self._config = config
  25. self._switch_dps = None
  26. self._power_dps = None
  27. self._attr_dps = []
  28. for d in config.dps():
  29. if d.name == "switch":
  30. self._switch_dps = d
  31. else:
  32. if d.name == "current_power_w":
  33. self._power_dps = d
  34. if not d.hidden:
  35. self._attr_dps.append(d)
  36. if self._switch_dps is None:
  37. raise AttributeError("A switch device must have a switch.")
  38. @property
  39. def should_poll(self):
  40. """Return the polling state."""
  41. return True
  42. @property
  43. def name(self):
  44. """Return the name of the device."""
  45. return self._device.name
  46. @property
  47. def friendly_name(self):
  48. """Return the friendly name for this entity."""
  49. return self._config.name
  50. @property
  51. def unique_id(self):
  52. """Return the unique id of the device."""
  53. return self._device.unique_id
  54. @property
  55. def device_info(self):
  56. """Return device information about the device."""
  57. return self._device.device_info
  58. @property
  59. def device_class(self):
  60. """Return the class of this device"""
  61. return (
  62. DEVICE_CLASS_OUTLET
  63. if self._config.device_class == "outlet"
  64. else DEVICE_CLASS_SWITCH
  65. )
  66. @property
  67. def is_on(self):
  68. """Return whether the switch is on or not."""
  69. is_switched_on = self._switch_dps.get_value(self._device)
  70. if is_switched_on is None:
  71. return STATE_UNAVAILABLE
  72. else:
  73. return is_switched_on
  74. @property
  75. def current_power_w(self):
  76. """Return the current power consumption in Watts."""
  77. if self._power_dps is None:
  78. return None
  79. pwr = self._power_dps.get_value(self._device)
  80. if pwr is None:
  81. return STATE_UNAVAILABLE
  82. return pwr
  83. @property
  84. def device_state_attributes(self):
  85. """Get additional attributes that HA doesn't naturally support."""
  86. attr = {}
  87. for a in self._attr_dps:
  88. attr[a.name] = a.get_value(self._device)
  89. return attr
  90. async def async_turn_on(self, **kwargs):
  91. """Turn the switch on"""
  92. await self._switch_dps.async_set_value(self._device, True)
  93. async def async_turn_off(self, **kwargs):
  94. """Turn the switch off"""
  95. await self._switch_dps.async_set_value(self._device, False)
  96. async def async_update(self):
  97. await self._device.async_refresh()