switch.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. PROPERTY_TO_DPS_ID,
  16. )
  17. class KoganSocketSwitch(SwitchEntity):
  18. """Representation of a Kogan WiFi-connected energy monitoring socket"""
  19. def __init__(self, device):
  20. """Initialize the switch.
  21. Args:
  22. device (TuyaLocalDevice): The device API instance."""
  23. self._device = device
  24. @property
  25. def should_poll(self):
  26. """Return the polling state."""
  27. return True
  28. @property
  29. def name(self):
  30. """Return the name of the switch."""
  31. return self._device.name
  32. @property
  33. def unique_id(self):
  34. """Return the unique id for this switch."""
  35. return self._device.unique_id
  36. @property
  37. def device_info(self):
  38. """Return device information about this switch."""
  39. return self._device.device_info
  40. @property
  41. def device_class(self):
  42. """Return the class of this device"""
  43. return DEVICE_CLASS_OUTLET
  44. @property
  45. def is_on(self):
  46. """Return the whether the switch is on."""
  47. is_switched_on = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_SWITCH])
  48. if is_switched_on is None:
  49. return STATE_UNAVAILABLE
  50. else:
  51. return is_switched_on
  52. @property
  53. def current_power_w(self):
  54. """Return the current power consumption in Watts"""
  55. pwr = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W])
  56. if pwr is None:
  57. return STATE_UNAVAILABLE
  58. else:
  59. return pwr / 10.0
  60. @property
  61. def device_state_attributes(self):
  62. """Get additional attributes that HA doesn't naturally support."""
  63. timer = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_TIMER])
  64. voltage = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_VOLTAGE_V])
  65. current = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_A])
  66. return {
  67. ATTR_CURRENT_POWER_W: self.current_power_w,
  68. ATTR_CURRENT_A: None if current is None else current / 1000.0,
  69. ATTR_VOLTAGE_V: None if voltage is None else voltage / 10.0,
  70. ATTR_TIMER: timer,
  71. }
  72. async def async_turn_on(self, **kwargs):
  73. """Turn the switch on"""
  74. await self._device.async_set_property(PROPERTY_TO_DPS_ID[ATTR_SWITCH], True)
  75. async def async_turn_off(self, **kwargs):
  76. """Turn the switch off"""
  77. await self._device.async_set_property(PROPERTY_TO_DPS_ID[ATTR_SWITCH], False)
  78. async def async_update(self):
  79. await self._device.async_refresh()