switch.py 3.1 KB

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