switch.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. return (
  59. self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W]) / 10.0
  60. )
  61. @property
  62. def device_state_attributes(self):
  63. """Get additional attributes that HA doesn't naturally support."""
  64. timer = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_TIMER])
  65. voltage = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_VOLTAGE_V]) / 10.0
  66. current = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_A]) / 1000.0
  67. return {
  68. ATTR_CURRENT_POWER_W: self.current_power_w,
  69. ATTR_CURRENT_A: current,
  70. ATTR_VOLTAGE_V: voltage,
  71. ATTR_TIMER: timer,
  72. }
  73. async def async_turn_on(self, **kwargs):
  74. """Turn the switch on"""
  75. await self._device.async_set_property(PROPERTY_TO_DPS_ID[ATTR_SWITCH], True)
  76. async def async_turn_off(self, **kwargs):
  77. """Turn the switch off"""
  78. await self._device.async_set_property(PROPERTY_TO_DPS_ID[ATTR_SWITCH], False)
  79. async def async_update(self):
  80. await self._device.async_refresh()