switch.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. if self.is_switched_on is None:
  51. return STATE_UNAVAILABLE
  52. else:
  53. return self.is_switched_on
  54. @property
  55. def current_power_w(self):
  56. """Return the current power consumption in Watts"""
  57. return (
  58. self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W]) / 10.0
  59. )
  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]) / 10.0
  65. current = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_CURRENT_A]) / 1000.0
  66. return {
  67. ATTR_CURRENT_POWER_W: self.current_power_w,
  68. ATTR_CURRENT_A: current,
  69. ATTR_VOLTAGE_V: voltage,
  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()