Add power and energy attributes to SmartThings switch (#21375)

This commit is contained in:
Andrew Sayre 2019-02-25 08:50:16 -06:00 committed by Martin Hjelmare
parent a50bcdff1a
commit 0ccbf61aea
2 changed files with 26 additions and 4 deletions

View File

@ -29,7 +29,9 @@ def get_capabilities(capabilities: Sequence[str]) -> Optional[Sequence[str]]:
# Must be able to be turned on/off. # Must be able to be turned on/off.
if Capability.switch in capabilities: if Capability.switch in capabilities:
return [Capability.switch] return [Capability.switch,
Capability.energy_meter,
Capability.power_meter]
return None return None
@ -50,6 +52,18 @@ class SmartThingsSwitch(SmartThingsEntity, SwitchDevice):
# the entity state ahead of receiving the confirming push updates # the entity state ahead of receiving the confirming push updates
self.async_schedule_update_ha_state() 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 @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if light is on.""" """Return true if light is on."""

View File

@ -9,7 +9,8 @@ from pysmartthings import Attribute, Capability
from homeassistant.components.smartthings import switch from homeassistant.components.smartthings import switch
from homeassistant.components.smartthings.const import ( from homeassistant.components.smartthings.const import (
DOMAIN, SIGNAL_SMARTTHINGS_UPDATE) 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 homeassistant.helpers.dispatcher import async_dispatcher_send
from .conftest import setup_platform 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): async def test_turn_on(hass, device_factory):
"""Test the switch turns of successfully.""" """Test the switch turns of successfully."""
# Arrange # Arrange
device = device_factory('Switch_1', [Capability.switch], device = device_factory('Switch_1',
{Attribute.switch: 'off'}) [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) await setup_platform(hass, SWITCH_DOMAIN, device)
# Act # Act
await hass.services.async_call( await hass.services.async_call(
@ -72,6 +78,8 @@ async def test_turn_on(hass, device_factory):
state = hass.states.get('switch.switch_1') state = hass.states.get('switch.switch_1')
assert state is not None assert state is not None
assert state.state == 'on' 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): async def test_update_from_signal(hass, device_factory):