Last active
February 9, 2020 18:55
-
-
Save AdamStrojek/4d7dbdea48f6e9e3efd8ca52b21c0864 to your computer and use it in GitHub Desktop.
This is temporary fix for TP-Link HS110 after firmware upgrade for Home Assistant. Just save this file to `custom_components/switch` folder in config folder and it will replace original component from Home Assistant. I have changed how requirements are provided so it will install current version of pyHS100 with required patches to work. No more …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Support for TPLink HS100/HS110/HS200 smart switch. | |
For more details about this platform, please refer to the documentation at | |
https://home-assistant.io/components/switch.tplink/ | |
""" | |
import logging | |
import time | |
import voluptuous as vol | |
from homeassistant.components.switch import ( | |
SwitchDevice, PLATFORM_SCHEMA, ATTR_CURRENT_POWER_W, ATTR_TODAY_ENERGY_KWH) | |
from homeassistant.const import (CONF_HOST, CONF_NAME, ATTR_VOLTAGE) | |
import homeassistant.helpers.config_validation as cv | |
REQUIREMENTS = ['pyHS100==0.3.2'] | |
_LOGGER = logging.getLogger(__name__) | |
ATTR_TOTAL_ENERGY_KWH = 'total_energy_kwh' | |
ATTR_CURRENT_A = 'current_a' | |
CONF_LEDS = 'enable_leds' | |
DEFAULT_NAME = 'TP-Link Switch' | |
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |
vol.Required(CONF_HOST): cv.string, | |
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, | |
vol.Optional(CONF_LEDS): cv.boolean, | |
}) | |
# pylint: disable=unused-argument | |
def setup_platform(hass, config, add_devices, discovery_info=None): | |
"""Set up the TPLink switch platform.""" | |
from pyHS100 import SmartPlug | |
host = config.get(CONF_HOST) | |
name = config.get(CONF_NAME) | |
leds_on = config.get(CONF_LEDS) | |
add_devices([SmartPlugSwitch(SmartPlug(host), name, leds_on)], True) | |
class SmartPlugSwitch(SwitchDevice): | |
"""Representation of a TPLink Smart Plug switch.""" | |
def __init__(self, smartplug, name, leds_on): | |
"""Initialize the switch.""" | |
self.smartplug = smartplug | |
self._name = name | |
if leds_on is not None: | |
self.smartplug.led = leds_on | |
self._state = None | |
self._available = True | |
# Set up emeter cache | |
self._emeter_params = {} | |
@property | |
def name(self): | |
"""Return the name of the Smart Plug, if any.""" | |
return self._name | |
@property | |
def available(self) -> bool: | |
"""Return if switch is available.""" | |
return self._available | |
@property | |
def is_on(self): | |
"""Return true if switch is on.""" | |
return self._state | |
def turn_on(self, **kwargs): | |
"""Turn the switch on.""" | |
self.smartplug.turn_on() | |
def turn_off(self): | |
"""Turn the switch off.""" | |
self.smartplug.turn_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.""" | |
from pyHS100 import SmartDeviceException | |
try: | |
self._available = True | |
self._state = self.smartplug.state == \ | |
self.smartplug.SWITCH_STATE_ON | |
if self._name is None: | |
self._name = self.smartplug.alias | |
if self.smartplug.has_emeter: | |
emeter_readings = self.smartplug.get_emeter_realtime() | |
for value in emeter_readings : | |
try : | |
name, unit = value.split("_",1) | |
div = 1000 | |
except ValueError: | |
name = value.split("_",1)[0] | |
div = 1 | |
if name == "power": | |
self._emeter_params[ATTR_CURRENT_POWER_W] \ | |
= "%.1f W" % (float(emeter_readings[value])/div) | |
if name == "total": | |
self._emeter_params[ATTR_TODAY_ENERGY_KWH] \ | |
= "%.2f kW" % (float(emeter_readings[value])/div) | |
if name == "voltage": | |
self._emeter_params[ATTR_VOLTAGE] \ | |
= "%.2f V" % (float(emeter_readings[value])/div) | |
if name == "current": | |
self._emeter_params[ATTR_CURRENT_A] \ | |
= "%.1f A" % (float(emeter_readings[value])/div) | |
try: | |
emeter_statics = self.smartplug.get_emeter_daily() | |
self._emeter_params[ATTR_TODAY_ENERGY_KWH] \ | |
= "%.2f kW" % (float(emeter_statics[int(time.strftime("%e"))]/1000)) | |
except KeyError: | |
# Device returned no daily history | |
pass | |
except (SmartDeviceException, OSError) as ex: | |
_LOGGER.warning("Could not read state for %s: %s", self.name, ex) | |
self._available = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is for a very old version of HA, when there were problems with an old version of library. This was fast fix to overcome this problem and I suggest using integration in new versions of HA. I'm currently using it and there are no problems with it.