From 0ccbf61aeaa76aacaa75d1bd0e2f5a908e297fb9 Mon Sep 17 00:00:00 2001 From: Andrew Sayre <6730289+andrewsayre@users.noreply.github.com> Date: Mon, 25 Feb 2019 08:50:16 -0600 Subject: [PATCH] Add power and energy attributes to SmartThings switch (#21375) --- homeassistant/components/smartthings/switch.py | 16 +++++++++++++++- tests/components/smartthings/test_switch.py | 14 +++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/smartthings/switch.py b/homeassistant/components/smartthings/switch.py index 5a1224f4fc2..d30aa3a2303 100644 --- a/homeassistant/components/smartthings/switch.py +++ b/homeassistant/components/smartthings/switch.py @@ -29,7 +29,9 @@ def get_capabilities(capabilities: Sequence[str]) -> Optional[Sequence[str]]: # Must be able to be turned on/off. if Capability.switch in capabilities: - return [Capability.switch] + return [Capability.switch, + Capability.energy_meter, + Capability.power_meter] return None @@ -50,6 +52,18 @@ class SmartThingsSwitch(SmartThingsEntity, SwitchDevice): # the entity state ahead of receiving the confirming push updates self.async_schedule_update_ha_state() + @property + def current_power_w(self): + """Return the current power usage in W.""" + from pysmartthings import Attribute + return self._device.status.attributes[Attribute.power].value + + @property + def today_energy_kwh(self): + """Return the today total energy usage in kWh.""" + from pysmartthings import Attribute + return self._device.status.attributes[Attribute.energy].value + @property def is_on(self) -> bool: """Return true if light is on.""" diff --git a/tests/components/smartthings/test_switch.py b/tests/components/smartthings/test_switch.py index 6ad87b7ad53..7d21db00460 100644 --- a/tests/components/smartthings/test_switch.py +++ b/tests/components/smartthings/test_switch.py @@ -9,7 +9,8 @@ from pysmartthings import Attribute, Capability from homeassistant.components.smartthings import switch from homeassistant.components.smartthings.const import ( DOMAIN, SIGNAL_SMARTTHINGS_UPDATE) -from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN +from homeassistant.components.switch import ( + ATTR_CURRENT_POWER_W, ATTR_TODAY_ENERGY_KWH, DOMAIN as SWITCH_DOMAIN) from homeassistant.helpers.dispatcher import async_dispatcher_send from .conftest import setup_platform @@ -61,8 +62,13 @@ async def test_turn_off(hass, device_factory): async def test_turn_on(hass, device_factory): """Test the switch turns of successfully.""" # Arrange - device = device_factory('Switch_1', [Capability.switch], - {Attribute.switch: 'off'}) + device = device_factory('Switch_1', + [Capability.switch, + Capability.power_meter, + Capability.energy_meter], + {Attribute.switch: 'off', + Attribute.power: 355, + Attribute.energy: 11.422}) await setup_platform(hass, SWITCH_DOMAIN, device) # Act await hass.services.async_call( @@ -72,6 +78,8 @@ async def test_turn_on(hass, device_factory): state = hass.states.get('switch.switch_1') assert state is not None assert state.state == 'on' + assert state.attributes[ATTR_CURRENT_POWER_W] == 355 + assert state.attributes[ATTR_TODAY_ENERGY_KWH] == 11.422 async def test_update_from_signal(hass, device_factory):