Adds energy monitoring capabilities to the TP-Link HS110 (#3917)

* Adds energy monitoring capabilities to the TP-Link HS110

Energy monitoring works only on the HS110 model

* Reverts to using GadgetReactor's module

* Updates requirements_all.txt

* Refactors tplink switch to use attribute caching

* Update tplink.py
This commit is contained in:
Georgi Kirichkov 2016-10-22 07:45:36 +03:00 committed by Paulus Schoutsen
parent 8d375e2d47
commit 4f6ed09a99
2 changed files with 37 additions and 6 deletions

View File

@ -12,13 +12,18 @@ from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_HOST, CONF_NAME)
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['https://github.com/gadgetreactor/pyHS100/archive/'
'ef85f939fd5b07064a0f34dfa673fa7d6140bd95.zip#pyHS100==0.1.2']
REQUIREMENTS = ['https://github.com/GadgetReactor/pyHS100/archive/'
'1f771b7d8090a91c6a58931532e42730b021cbde.zip#pyHS100==0.2.0']
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'TPLink Switch HS100'
ATTR_CURRENT_CONSUMPTION = 'Current consumption'
ATTR_TOTAL_CONSUMPTION = 'Total consumption'
ATTR_VOLTAGE = 'Voltage'
ATTR_CURRENT = 'Current'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
@ -42,6 +47,11 @@ class SmartPlugSwitch(SwitchDevice):
"""Initialize the switch."""
self.smartplug = smartplug
self._name = name
self._state = None
self._emeter_present = (smartplug.model == 110)
_LOGGER.debug("Setting up TP-Link Smart Plug HS%i", smartplug.model)
# Set up emeter cache
self._emeter_params = {}
@property
def name(self):
@ -51,7 +61,7 @@ class SmartPlugSwitch(SwitchDevice):
@property
def is_on(self):
"""Return true if switch is on."""
return self.smartplug.state == 'ON'
return self._state == 'ON'
def turn_on(self, **kwargs):
"""Turn the switch on."""
@ -60,3 +70,24 @@ class SmartPlugSwitch(SwitchDevice):
def turn_off(self):
"""Turn the switch off."""
self.smartplug.state = 'OFF'
@property
def device_state_attributes(self):
"""Return the state attributes of the device."""
return self._emeter_params
def update(self):
"""Update the TP-Link switch's state."""
self._state = self.smartplug.state
if self._emeter_present:
emeter_readings = self.smartplug.get_emeter_realtime()
self._emeter_params[ATTR_CURRENT_CONSUMPTION] \
= "%.1f W" % emeter_readings["power"]
self._emeter_params[ATTR_TOTAL_CONSUMPTION] \
= "%.2f kW" % emeter_readings["total"]
self._emeter_params[ATTR_VOLTAGE] \
= "%.2f V" % emeter_readings["voltage"]
self._emeter_params[ATTR_CURRENT] \
= "%.1f A" % emeter_readings["current"]

View File

@ -154,6 +154,9 @@ hikvision==0.4
# homeassistant.components.light.flux_led
https://github.com/Danielhiversen/flux_led/archive/0.7.zip#flux_led==0.8
# homeassistant.components.switch.tplink
https://github.com/GadgetReactor/pyHS100/archive/1f771b7d8090a91c6a58931532e42730b021cbde.zip#pyHS100==0.2.0
# homeassistant.components.switch.dlink
https://github.com/LinuxChristian/pyW215/archive/v0.3.5.zip#pyW215==0.3.5
@ -183,9 +186,6 @@ https://github.com/danieljkemp/onkyo-eiscp/archive/python3.zip#onkyo-eiscp==0.9.
# homeassistant.components.device_tracker.fritz
# https://github.com/deisi/fritzconnection/archive/b5c14515e1c8e2652b06b6316a7f3913df942841.zip#fritzconnection==0.4.6
# homeassistant.components.switch.tplink
https://github.com/gadgetreactor/pyHS100/archive/ef85f939fd5b07064a0f34dfa673fa7d6140bd95.zip#pyHS100==0.1.2
# homeassistant.components.netatmo
https://github.com/jabesq/netatmo-api-python/archive/v0.6.0.zip#lnetatmo==0.6.0