Add support for automatic discovery of TP-Link switches, bulbs and dimmers (#18091)
* {switch,light}.tplink: use deviceid as unique id, fetch name from the device during initialization
* raise PlatformNotReady when no device is available
* Use mac instead of deviceid
* remove name option as obsolete
* Add support for configuration flow / integration
Allows activating automatic discovery of supported devices from the configuration
* Fix linting, update requirements_all.txt
* start cleaning up tplink component based on feedback
* add device info, improve config handling
* Allow overriding detected devices via configuration file
* Update requirements.txt
* Remove debug logging
* make hound happy
* Avoid I/O during init and simplify the code, remove remains of leds_on
* Fix issues based on feedback, use consistent quotation marks for device info
* add async_setup_platform emiting a deprecation warning
* Avoid blocking the I/O, check for None on features
* handle some Martin's comments, schema-validation is still missing
* use async_create_task instead of async_add_job, let core validate the schema
* simplify configuration handling by storing the configuration data separately from initialized instances
* add default values to schema, make hound happy
* with defaults set by schema, simplify the checks. add async_unload_entry
* Use constant for data structure access
* REWORD add a short note about async_unload_entry
* handle feedback from Martin, config_data is checked against Noneness
* use pop to remove the domain on unload
* First steps to add tests for the new tplink component
* embed platforms under the component directory
* Fix tests by mocking the pyhs100 internals
* Fix linting
* Test against multiple instances of devices, tidy up
* (hopefully) final linting round
* Add pyHS100 to test requirements
* log always the warnings occured during an update to make them easy to see
* revert back the warning behavior (requirement for silver level in IQS)
* Unload only when an entry is being loaded and add tests for that
Thanks @MartinHjelmare for pointing this out!
* Fix linting
* Bump the upstream lib, fixes most prominently the HSV setting on bulbs
* Test unloading for all platforms, clear the data storage instead of popping it out, making it possible to reconfigure after removal without restarting hass first
* Use class variables instead of instance variables for bulb states, required for HS220
* Use new-style format string
* Fix indenting, uppercase the mock constant
* Run black on test_init, hopefully that will finally fix the weird formatting (pycharm, pylint and hound seems to have different opinions...)
This commit is contained in:
1 parent
c637bad1eb
commit
94be43e3e1
11 files changed
+484
-76
No files matched your search
@@ -1,128 +0,0 @@
|
||||
"""
|
||||
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.4']
|
||||
|
||||
_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,
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, 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_entities([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
|
||||
self._leds_on = 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, **kwargs):
|
||||
"""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._state = self.smartplug.state == \
|
||||
self.smartplug.SWITCH_STATE_ON
|
||||
|
||||
if self._leds_on is not None:
|
||||
self.smartplug.led = self._leds_on
|
||||
self._leds_on = None
|
||||
|
||||
# Pull the name from the device if a name was not specified
|
||||
if self._name == DEFAULT_NAME:
|
||||
self._name = self.smartplug.alias
|
||||
|
||||
if self.smartplug.has_emeter:
|
||||
emeter_readings = self.smartplug.get_emeter_realtime()
|
||||
|
||||
self._emeter_params[ATTR_CURRENT_POWER_W] \
|
||||
= "{:.2f}".format(emeter_readings["power"])
|
||||
self._emeter_params[ATTR_TOTAL_ENERGY_KWH] \
|
||||
= "{:.3f}".format(emeter_readings["total"])
|
||||
self._emeter_params[ATTR_VOLTAGE] \
|
||||
= "{:.1f}".format(emeter_readings["voltage"])
|
||||
self._emeter_params[ATTR_CURRENT_A] \
|
||||
= "{:.2f}".format(emeter_readings["current"])
|
||||
|
||||
emeter_statics = self.smartplug.get_emeter_daily()
|
||||
try:
|
||||
self._emeter_params[ATTR_TODAY_ENERGY_KWH] \
|
||||
= "{:.3f}".format(
|
||||
emeter_statics[int(time.strftime("%e"))])
|
||||
except KeyError:
|
||||
# Device returned no daily history
|
||||
pass
|
||||
|
||||
self._available = True
|
||||
|
||||
except (SmartDeviceException, OSError) as ex:
|
||||
if self._available:
|
||||
_LOGGER.warning(
|
||||
"Could not read state for %s: %s", self.name, ex)
|
||||
self._available = False
|
||||
Reference in new issue
Block a user