switch.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. Platform to control the switch on Kogan WiFi-connected energy monitoring sockets.
  3. """
  4. from homeassistant.components.switch import SwitchEntity
  5. from homeassistant.components.switch import (
  6. ATTR_CURRENT_POWER_W,
  7. DEVICE_CLASS_OUTLET,
  8. )
  9. from homeassistant.const import STATE_UNAVAILABLE
  10. from .const import (
  11. ATTR_CURRENT_A,
  12. ATTR_SWITCH,
  13. ATTR_TIMER,
  14. ATTR_VOLTAGE_V,
  15. ATTR_ALT_TIMER,
  16. ATTR_ALT_CURRENT_A,
  17. ATTR_ALT_CURRENT_POWER_W,
  18. ATTR_ALT_VOLTAGE_V,
  19. PROPERTY_TO_DPS_ID,
  20. )
  21. class KoganSocketSwitch(SwitchEntity):
  22. """Representation of a Kogan WiFi-connected energy monitoring socket"""
  23. def __init__(self, device):
  24. """Initialize the switch.
  25. Args:
  26. device (TuyaLocalDevice): The device API instance."""
  27. self._device = device
  28. @property
  29. def should_poll(self):
  30. """Return the polling state."""
  31. return True
  32. @property
  33. def name(self):
  34. """Return the name of the switch."""
  35. return self._device.name
  36. @property
  37. def unique_id(self):
  38. """Return the unique id for this switch."""
  39. return self._device.unique_id
  40. @property
  41. def device_info(self):
  42. """Return device information about this switch."""
  43. return self._device.device_info
  44. @property
  45. def device_class(self):
  46. """Return the class of this device"""
  47. return DEVICE_CLASS_OUTLET
  48. @property
  49. def is_on(self):
  50. """Return the whether the switch is on."""
  51. is_switched_on = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_SWITCH])
  52. if is_switched_on is None:
  53. return STATE_UNAVAILABLE
  54. else:
  55. return is_switched_on
  56. @property
  57. def current_power_w(self):
  58. """Return the current power consumption in Watts"""
  59. pwr = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W])
  60. if pwr is None:
  61. # Some newer plugs have the measurements on different DPS ids
  62. pwr = self._device.get_property(
  63. PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_POWER_W]
  64. )
  65. if pwr is None:
  66. return STATE_UNAVAILABLE
  67. return pwr / 10.0
  68. @property
  69. def device_state_attributes(self):
  70. """Get additional attributes that HA doesn't naturally support."""
  71. timer = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_TIMER])
  72. voltage = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_VOLTAGE_V])
  73. current = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_A])
  74. # Some newer plugs have the measurements on different DPS ids
  75. if timer is None:
  76. timer = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_ALT_TIMER])
  77. if voltage is None:
  78. voltage = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_ALT_VOLTAGE_V])
  79. if current is None:
  80. current = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_A])
  81. return {
  82. ATTR_CURRENT_POWER_W: self.current_power_w,
  83. ATTR_CURRENT_A: None if current is None else current / 1000.0,
  84. ATTR_VOLTAGE_V: None if voltage is None else voltage / 10.0,
  85. ATTR_TIMER: timer,
  86. }
  87. async def async_turn_on(self, **kwargs):
  88. """Turn the switch on"""
  89. await self._device.async_set_property(PROPERTY_TO_DPS_ID[ATTR_SWITCH], True)
  90. async def async_turn_off(self, **kwargs):
  91. """Turn the switch off"""
  92. await self._device.async_set_property(PROPERTY_TO_DPS_ID[ATTR_SWITCH], False)
  93. async def async_update(self):
  94. await self._device.async_refresh()