2019-04-03 15:40:03 +00:00
|
|
|
"""Support for TPLink HS100/HS110/HS200 smart switch."""
|
2020-10-11 19:10:36 +00:00
|
|
|
import asyncio
|
2016-09-04 02:09:02 +00:00
|
|
|
import logging
|
2016-11-11 06:43:16 +00:00
|
|
|
import time
|
|
|
|
|
2019-05-31 05:51:04 +00:00
|
|
|
from pyHS100 import SmartDeviceException, SmartPlug
|
|
|
|
|
2017-12-29 12:13:15 +00:00
|
|
|
from homeassistant.components.switch import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CURRENT_POWER_W,
|
|
|
|
ATTR_TODAY_ENERGY_KWH,
|
2020-04-26 16:50:37 +00:00
|
|
|
SwitchEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
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...)
2019-02-21 19:29:07 +00:00
|
|
|
from homeassistant.const import ATTR_VOLTAGE
|
2020-10-27 09:02:16 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
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...)
2019-02-21 19:29:07 +00:00
|
|
|
import homeassistant.helpers.device_registry as dr
|
2019-05-31 05:51:04 +00:00
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
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...)
2019-02-21 19:29:07 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import CONF_SWITCH, DOMAIN as TPLINK_DOMAIN
|
2020-10-27 09:02:16 +00:00
|
|
|
from .common import add_available_devices
|
2019-03-21 05:56:46 +00:00
|
|
|
|
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...)
2019-02-21 19:29:07 +00:00
|
|
|
PARALLEL_UPDATES = 0
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2016-09-04 02:09:02 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_TOTAL_ENERGY_KWH = "total_energy_kwh"
|
|
|
|
ATTR_CURRENT_A = "current_a"
|
2016-10-22 04:45:36 +00:00
|
|
|
|
2020-10-11 19:10:36 +00:00
|
|
|
MAX_ATTEMPTS = 300
|
|
|
|
SLEEP_TIME = 2
|
2019-05-31 05:51:04 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistantType, config_entry, async_add_entities):
|
2019-05-31 05:51:04 +00:00
|
|
|
"""Set up switches."""
|
2020-10-27 09:02:16 +00:00
|
|
|
await hass.async_add_executor_job(
|
|
|
|
add_available_devices, hass, CONF_SWITCH, SmartPlugSwitch, async_add_entities
|
|
|
|
)
|
2020-10-11 19:10:36 +00:00
|
|
|
|
2020-10-27 09:02:16 +00:00
|
|
|
if hass.data[TPLINK_DOMAIN][f"{CONF_SWITCH}_remaining"]:
|
|
|
|
raise PlatformNotReady
|
2016-07-10 16:48:02 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class SmartPlugSwitch(SwitchEntity):
|
2016-07-10 16:48:02 +00:00
|
|
|
"""Representation of a TPLink Smart Plug switch."""
|
|
|
|
|
2019-05-31 05:51:04 +00:00
|
|
|
def __init__(self, smartplug: SmartPlug):
|
2016-07-10 16:48:02 +00:00
|
|
|
"""Initialize the switch."""
|
|
|
|
self.smartplug = smartplug
|
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...)
2019-02-21 19:29:07 +00:00
|
|
|
self._sysinfo = None
|
2016-10-22 04:45:36 +00:00
|
|
|
self._state = None
|
2020-10-11 19:10:36 +00:00
|
|
|
self._is_available = False
|
2016-10-22 04:45:36 +00:00
|
|
|
# Set up emeter cache
|
|
|
|
self._emeter_params = {}
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2019-05-31 05:51:04 +00:00
|
|
|
self._mac = None
|
|
|
|
self._alias = None
|
|
|
|
self._model = None
|
2019-10-23 05:46:18 +00:00
|
|
|
self._device_id = None
|
2020-10-11 19:10:36 +00:00
|
|
|
self._host = None
|
2019-05-31 05:51:04 +00:00
|
|
|
|
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...)
2019-02-21 19:29:07 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
2019-10-23 05:46:18 +00:00
|
|
|
return self._device_id
|
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...)
2019-02-21 19:29:07 +00:00
|
|
|
|
2016-07-10 16:48:02 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
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...)
2019-02-21 19:29:07 +00:00
|
|
|
"""Return the name of the Smart Plug."""
|
2019-05-31 05:51:04 +00:00
|
|
|
return self._alias
|
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...)
2019-02-21 19:29:07 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return information about the device."""
|
|
|
|
return {
|
2019-05-31 05:51:04 +00:00
|
|
|
"name": self._alias,
|
|
|
|
"model": self._model,
|
2019-07-31 19:25:30 +00:00
|
|
|
"manufacturer": "TP-Link",
|
|
|
|
"connections": {(dr.CONNECTION_NETWORK_MAC, self._mac)},
|
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...)
2019-02-21 19:29:07 +00:00
|
|
|
"sw_version": self._sysinfo["sw_ver"],
|
|
|
|
}
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2017-12-04 07:33:22 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if switch is available."""
|
2020-10-11 19:10:36 +00:00
|
|
|
return self._is_available
|
2017-12-04 07:33:22 +00:00
|
|
|
|
2016-07-10 16:48:02 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if switch is on."""
|
2017-01-28 06:45:57 +00:00
|
|
|
return self._state
|
2016-07-10 16:48:02 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the switch on."""
|
2016-12-17 21:49:43 +00:00
|
|
|
self.smartplug.turn_on()
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2016-07-10 16:48:02 +00:00
|
|
|
"""Turn the switch off."""
|
2016-12-17 21:49:43 +00:00
|
|
|
self.smartplug.turn_off()
|
2016-10-22 04:45:36 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the device."""
|
|
|
|
return self._emeter_params
|
|
|
|
|
2019-11-26 07:03:38 +00:00
|
|
|
@property
|
|
|
|
def _plug_from_context(self):
|
|
|
|
"""Return the plug from the context."""
|
|
|
|
children = self.smartplug.sys_info["children"]
|
|
|
|
return next(c for c in children if c["id"] == self.smartplug.context)
|
|
|
|
|
2020-10-11 19:10:36 +00:00
|
|
|
def update_state(self):
|
2016-10-22 04:45:36 +00:00
|
|
|
"""Update the TP-Link switch's state."""
|
2020-10-11 19:10:36 +00:00
|
|
|
if self.smartplug.context is None:
|
|
|
|
self._state = self.smartplug.state == self.smartplug.SWITCH_STATE_ON
|
|
|
|
else:
|
|
|
|
self._state = self._plug_from_context["state"] == 1
|
|
|
|
|
|
|
|
def attempt_update(self, update_attempt):
|
|
|
|
"""Attempt to get details from the TP-Link switch."""
|
2016-11-11 06:43:16 +00:00
|
|
|
try:
|
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...)
2019-02-21 19:29:07 +00:00
|
|
|
if not self._sysinfo:
|
|
|
|
self._sysinfo = self.smartplug.sys_info
|
2020-10-11 19:10:36 +00:00
|
|
|
self._mac = self._sysinfo["mac"]
|
|
|
|
self._model = self._sysinfo["model"]
|
|
|
|
self._host = self.smartplug.host
|
2019-10-23 05:46:18 +00:00
|
|
|
if self.smartplug.context is None:
|
2020-10-11 19:10:36 +00:00
|
|
|
self._alias = self._sysinfo["alias"]
|
2019-10-23 05:46:18 +00:00
|
|
|
self._device_id = self._mac
|
|
|
|
else:
|
2019-11-26 07:03:38 +00:00
|
|
|
self._alias = self._plug_from_context["alias"]
|
2019-10-23 05:46:18 +00:00
|
|
|
self._device_id = self.smartplug.context
|
|
|
|
|
2020-10-11 19:10:36 +00:00
|
|
|
self.update_state()
|
2016-11-11 06:43:16 +00:00
|
|
|
|
2016-12-17 21:49:43 +00:00
|
|
|
if self.smartplug.has_emeter:
|
2016-11-11 06:43:16 +00:00
|
|
|
emeter_readings = self.smartplug.get_emeter_realtime()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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"]
|
|
|
|
)
|
2016-11-11 06:43:16 +00:00
|
|
|
|
|
|
|
emeter_statics = self.smartplug.get_emeter_daily()
|
2016-11-22 03:34:48 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._emeter_params[ATTR_TODAY_ENERGY_KWH] = "{:.3f}".format(
|
|
|
|
emeter_statics[int(time.strftime("%e"))]
|
|
|
|
)
|
2016-11-22 03:34:48 +00:00
|
|
|
except KeyError:
|
2017-12-04 07:33:22 +00:00
|
|
|
# Device returned no daily history
|
2016-11-22 03:34:48 +00:00
|
|
|
pass
|
2020-10-11 19:10:36 +00:00
|
|
|
return True
|
|
|
|
except (SmartDeviceException, OSError) as ex:
|
|
|
|
if update_attempt == 0:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Retrying in %s seconds for %s|%s due to: %s",
|
|
|
|
SLEEP_TIME,
|
|
|
|
self._host,
|
|
|
|
self._alias,
|
|
|
|
ex,
|
|
|
|
)
|
|
|
|
return False
|
2016-11-11 06:43:16 +00:00
|
|
|
|
2020-10-11 19:10:36 +00:00
|
|
|
async def async_update(self):
|
|
|
|
"""Update the TP-Link switch's state."""
|
|
|
|
for update_attempt in range(MAX_ATTEMPTS):
|
|
|
|
is_ready = await self.hass.async_add_executor_job(
|
|
|
|
self.attempt_update, update_attempt
|
|
|
|
)
|
|
|
|
|
|
|
|
if is_ready:
|
|
|
|
self._is_available = True
|
|
|
|
if update_attempt > 0:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Device %s|%s responded after %s attempts",
|
|
|
|
self._host,
|
|
|
|
self._alias,
|
|
|
|
update_attempt,
|
|
|
|
)
|
|
|
|
break
|
|
|
|
await asyncio.sleep(SLEEP_TIME)
|
2018-07-21 22:51:45 +00:00
|
|
|
|
2020-10-11 19:10:36 +00:00
|
|
|
else:
|
|
|
|
if self._is_available:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning(
|
2020-10-11 19:10:36 +00:00
|
|
|
"Could not read state for %s|%s", self.smartplug.host, self._alias
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-10-11 19:10:36 +00:00
|
|
|
self._is_available = False
|