switch.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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._attr_dps = []
  26. for d in config.dps():
  27. if d.name == "switch":
  28. self._switch_dps = d
  29. else:
  30. if d.name == "current_power_w":
  31. self._power_dps = d
  32. self._attr_dps.append(d)
  33. @property
  34. def should_poll(self):
  35. """Return the polling state."""
  36. return True
  37. @property
  38. def name(self):
  39. """Return the name of the device."""
  40. return self._device.name
  41. @property
  42. def friendly_name(self):
  43. """Return the friendly name for this entity."""
  44. return self._config.name
  45. @property
  46. def unique_id(self):
  47. """Return the unique id of the device."""
  48. return self._device.unique_id
  49. @property
  50. def device_info(self):
  51. """Return device information about the device."""
  52. return self._device.device_info
  53. @property
  54. def device_class(self):
  55. """Return the class of this device"""
  56. return (
  57. DEVICE_CLASS_OUTLET
  58. if self._config.device_class == "outlet"
  59. else DEVICE_CLASS_SWITCH
  60. )
  61. @property
  62. def is_on(self):
  63. """Return whether the switch is on or not."""
  64. is_switched_on = self._switch_dps.get_value(self._device)
  65. if is_switched_on is None:
  66. return STATE_UNAVAILABLE
  67. else:
  68. return is_switched_on
  69. @property
  70. def current_power_w(self):
  71. """Return the current power consumption in Watts."""
  72. if self._power_dps is None:
  73. return None
  74. pwr = self._power_dps.get_value(self._device)
  75. if pwr is None:
  76. return STATE_UNAVAILABLE
  77. return pwr
  78. @property
  79. def device_state_attributes(self):
  80. """Get additional attributes that HA doesn't naturally support."""
  81. attr = {}
  82. for a in self._attr_dps:
  83. attr[a.name] = a.get_value(self._device)
  84. return attr
  85. async def async_turn_on(self, **kwargs):
  86. """Turn the switch on"""
  87. await self._switch_dps.async_set_value(self._device, True)
  88. async def async_turn_off(self, **kwargs):
  89. """Turn the switch off"""
  90. await self._switch_dps.async_set_value(self._device, False)
  91. async def async_update(self):
  92. await self._device.async_refresh()