switch.py 3.5 KB

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