2019-04-03 15:40:03 +00:00
|
|
|
"""Support for TPLink lights."""
|
2020-01-12 02:45:01 +00:00
|
|
|
from datetime import timedelta
|
2017-07-26 07:04:40 +00:00
|
|
|
import logging
|
2017-10-19 04:52:44 +00:00
|
|
|
import time
|
2020-01-12 02:45:01 +00:00
|
|
|
from typing import Any, Dict, NamedTuple, Tuple, cast
|
2017-10-19 04:52:44 +00:00
|
|
|
|
2019-05-31 05:51:04 +00:00
|
|
|
from pyHS100 import SmartBulb, SmartDeviceException
|
|
|
|
|
2017-07-26 07:04:40 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
|
|
|
SUPPORT_COLOR_TEMP,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-04-08 19:36:45 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
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
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.util.color import (
|
|
|
|
color_temperature_kelvin_to_mired as kelvin_to_mired,
|
2019-07-31 19:25:30 +00:00
|
|
|
color_temperature_mired_to_kelvin as mired_to_kelvin,
|
|
|
|
)
|
2020-04-08 19:36:45 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from . import CONF_LIGHT, DOMAIN as TPLINK_DOMAIN
|
2019-05-31 05:51:04 +00:00
|
|
|
from .common import async_add_entities_retry
|
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
|
2020-01-12 02:45:01 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=5)
|
2020-04-08 19:36:45 +00:00
|
|
|
CURRENT_POWER_UPDATE_INTERVAL = timedelta(seconds=60)
|
|
|
|
HISTORICAL_POWER_UPDATE_INTERVAL = timedelta(minutes=60)
|
2017-07-26 07:04:40 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CURRENT_POWER_W = "current_power_w"
|
|
|
|
ATTR_DAILY_ENERGY_KWH = "daily_energy_kwh"
|
|
|
|
ATTR_MONTHLY_ENERGY_KWH = "monthly_energy_kwh"
|
2017-07-26 07:04:40 +00:00
|
|
|
|
2020-04-08 19:36:45 +00:00
|
|
|
LIGHT_STATE_DFT_ON = "dft_on_state"
|
|
|
|
LIGHT_STATE_ON_OFF = "on_off"
|
2020-04-10 00:10:02 +00:00
|
|
|
LIGHT_STATE_RELAY_STATE = "relay_state"
|
2020-04-08 19:36:45 +00:00
|
|
|
LIGHT_STATE_BRIGHTNESS = "brightness"
|
|
|
|
LIGHT_STATE_COLOR_TEMP = "color_temp"
|
|
|
|
LIGHT_STATE_HUE = "hue"
|
|
|
|
LIGHT_STATE_SATURATION = "saturation"
|
|
|
|
LIGHT_STATE_ERROR_MSG = "err_msg"
|
|
|
|
|
|
|
|
LIGHT_SYSINFO_MAC = "mac"
|
|
|
|
LIGHT_SYSINFO_ALIAS = "alias"
|
|
|
|
LIGHT_SYSINFO_MODEL = "model"
|
|
|
|
LIGHT_SYSINFO_IS_DIMMABLE = "is_dimmable"
|
|
|
|
LIGHT_SYSINFO_IS_VARIABLE_COLOR_TEMP = "is_variable_color_temp"
|
|
|
|
LIGHT_SYSINFO_IS_COLOR = "is_color"
|
|
|
|
|
2017-12-28 20:22:46 +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."""
|
|
|
|
await async_add_entities_retry(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, async_add_entities, hass.data[TPLINK_DOMAIN][CONF_LIGHT], add_entity
|
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
|
|
|
return True
|
2017-07-26 07:04:40 +00:00
|
|
|
|
|
|
|
|
2019-05-31 05:51:04 +00:00
|
|
|
def add_entity(device: SmartBulb, async_add_entities):
|
|
|
|
"""Check if device is online and add the entity."""
|
|
|
|
# Attempt to get the sysinfo. If it fails, it will raise an
|
|
|
|
# exception that is caught by async_add_entities_retry which
|
|
|
|
# will try again later.
|
|
|
|
device.get_sysinfo()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_add_entities([TPLinkSmartBulb(device)], update_before_add=True)
|
2019-05-31 05:51:04 +00:00
|
|
|
|
|
|
|
|
2017-07-26 07:04:40 +00:00
|
|
|
def brightness_to_percentage(byt):
|
|
|
|
"""Convert brightness from absolute 0..255 to percentage."""
|
2020-04-14 18:48:35 +00:00
|
|
|
return round((byt * 100.0) / 255.0)
|
2017-07-26 07:04:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def brightness_from_percentage(percent):
|
|
|
|
"""Convert percentage to absolute value 0..255."""
|
2020-04-14 18:48:35 +00:00
|
|
|
return round((percent * 255.0) / 100.0)
|
2017-07-26 07:04:40 +00:00
|
|
|
|
|
|
|
|
2020-03-21 09:18:32 +00:00
|
|
|
class LightState(NamedTuple):
|
|
|
|
"""Light state."""
|
|
|
|
|
|
|
|
state: bool
|
|
|
|
brightness: int
|
|
|
|
color_temp: float
|
|
|
|
hs: Tuple[int, int]
|
2020-04-08 19:36:45 +00:00
|
|
|
|
|
|
|
def to_param(self):
|
|
|
|
"""Return a version that we can send to the bulb."""
|
|
|
|
if self.color_temp:
|
|
|
|
color_temp = mired_to_kelvin(self.color_temp)
|
|
|
|
else:
|
|
|
|
color_temp = None
|
|
|
|
|
|
|
|
return {
|
|
|
|
LIGHT_STATE_ON_OFF: 1 if self.state else 0,
|
|
|
|
LIGHT_STATE_BRIGHTNESS: brightness_to_percentage(self.brightness),
|
|
|
|
LIGHT_STATE_COLOR_TEMP: color_temp,
|
|
|
|
LIGHT_STATE_HUE: self.hs[0] if self.hs else 0,
|
|
|
|
LIGHT_STATE_SATURATION: self.hs[1] if self.hs else 0,
|
|
|
|
}
|
2020-03-21 09:18:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LightFeatures(NamedTuple):
|
|
|
|
"""Light features."""
|
|
|
|
|
|
|
|
sysinfo: Dict[str, Any]
|
|
|
|
mac: str
|
|
|
|
alias: str
|
|
|
|
model: str
|
|
|
|
supported_features: int
|
|
|
|
min_mireds: float
|
|
|
|
max_mireds: float
|
2020-04-10 00:10:02 +00:00
|
|
|
has_emeter: bool
|
2020-01-12 02:45:01 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class TPLinkSmartBulb(LightEntity):
|
2017-07-26 07:04:40 +00:00
|
|
|
"""Representation of a TPLink Smart Bulb."""
|
|
|
|
|
2019-05-31 05:51:04 +00:00
|
|
|
def __init__(self, smartbulb: SmartBulb) -> None:
|
2017-07-26 07:04:40 +00:00
|
|
|
"""Initialize the bulb."""
|
|
|
|
self.smartbulb = smartbulb
|
2020-01-12 02:45:01 +00:00
|
|
|
self._light_features = cast(LightFeatures, None)
|
|
|
|
self._light_state = cast(LightState, None)
|
|
|
|
self._is_available = True
|
|
|
|
self._is_setting_light_state = False
|
2020-04-08 19:36:45 +00:00
|
|
|
self._last_current_power_update = None
|
|
|
|
self._last_historical_power_update = None
|
|
|
|
self._emeter_params = {}
|
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."""
|
2020-01-12 02:45:01 +00:00
|
|
|
return self._light_features.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
|
|
|
|
2017-07-26 07:04:40 +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 Bulb."""
|
2020-01-12 02:45:01 +00:00
|
|
|
return self._light_features.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 {
|
2020-01-12 02:45:01 +00:00
|
|
|
"name": self._light_features.alias,
|
|
|
|
"model": self._light_features.model,
|
2019-07-31 19:25:30 +00:00
|
|
|
"manufacturer": "TP-Link",
|
2020-01-12 02:45:01 +00:00
|
|
|
"connections": {(dr.CONNECTION_NETWORK_MAC, self._light_features.mac)},
|
|
|
|
"sw_version": self._light_features.sysinfo["sw_ver"],
|
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
|
|
|
}
|
2017-07-26 07:04:40 +00:00
|
|
|
|
2017-12-12 16:18:46 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if bulb is available."""
|
2020-01-12 02:45:01 +00:00
|
|
|
return self._is_available
|
2017-12-12 16:18:46 +00:00
|
|
|
|
2017-10-19 04:52:44 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the device."""
|
2020-04-08 19:36:45 +00:00
|
|
|
return self._emeter_params
|
2017-10-19 04:52:44 +00:00
|
|
|
|
2020-01-12 02:45:01 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2017-07-26 07:04:40 +00:00
|
|
|
"""Turn the light on."""
|
2020-04-08 19:36:45 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
|
|
|
brightness = int(kwargs[ATTR_BRIGHTNESS])
|
|
|
|
elif self._light_state.brightness is not None:
|
|
|
|
brightness = self._light_state.brightness
|
|
|
|
else:
|
|
|
|
brightness = 255
|
2020-01-12 02:45:01 +00:00
|
|
|
|
2020-04-08 19:36:45 +00:00
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
|
|
|
color_tmp = int(kwargs[ATTR_COLOR_TEMP])
|
|
|
|
else:
|
|
|
|
color_tmp = self._light_state.color_temp
|
|
|
|
|
|
|
|
if ATTR_HS_COLOR in kwargs:
|
|
|
|
# TP-Link requires integers.
|
|
|
|
hue_sat = tuple(int(val) for val in kwargs[ATTR_HS_COLOR])
|
|
|
|
|
|
|
|
# TP-Link cannot have both color temp and hue_sat
|
|
|
|
color_tmp = 0
|
|
|
|
else:
|
|
|
|
hue_sat = self._light_state.hs
|
|
|
|
|
|
|
|
await self._async_set_light_state_retry(
|
2020-01-12 02:45:01 +00:00
|
|
|
self._light_state,
|
2020-04-08 19:36:45 +00:00
|
|
|
self._light_state._replace(
|
2020-08-27 11:56:20 +00:00
|
|
|
state=True,
|
|
|
|
brightness=brightness,
|
|
|
|
color_temp=color_tmp,
|
|
|
|
hs=hue_sat,
|
2020-01-12 02:45:01 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-07-26 07:04:40 +00:00
|
|
|
"""Turn the light off."""
|
2020-04-08 19:36:45 +00:00
|
|
|
await self._async_set_light_state_retry(
|
2020-08-27 11:56:20 +00:00
|
|
|
self._light_state,
|
|
|
|
self._light_state._replace(state=False),
|
2020-01-12 02:45:01 +00:00
|
|
|
)
|
2017-07-26 07:04:40 +00:00
|
|
|
|
2018-06-20 17:29:36 +00:00
|
|
|
@property
|
|
|
|
def min_mireds(self):
|
|
|
|
"""Return minimum supported color temperature."""
|
2020-01-12 02:45:01 +00:00
|
|
|
return self._light_features.min_mireds
|
2018-06-20 17:29:36 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def max_mireds(self):
|
|
|
|
"""Return maximum supported color temperature."""
|
2020-01-12 02:45:01 +00:00
|
|
|
return self._light_features.max_mireds
|
2018-06-20 17:29:36 +00:00
|
|
|
|
2017-07-26 07:04:40 +00:00
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Return the color temperature of this light in mireds for HA."""
|
2020-01-12 02:45:01 +00:00
|
|
|
return self._light_state.color_temp
|
2017-07-26 07:04:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
2020-01-12 02:45:01 +00:00
|
|
|
return self._light_state.brightness
|
2017-07-26 07:04:40 +00:00
|
|
|
|
2017-08-23 05:11:44 +00:00
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def hs_color(self):
|
|
|
|
"""Return the color."""
|
2020-01-12 02:45:01 +00:00
|
|
|
return self._light_state.hs
|
2017-08-23 05:11:44 +00:00
|
|
|
|
2017-07-26 07:04:40 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
2017-10-19 04:52:44 +00:00
|
|
|
"""Return True if device is on."""
|
2020-01-12 02:45:01 +00:00
|
|
|
return self._light_state.state
|
2017-07-26 07:04:40 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the TP-Link Bulb's state."""
|
2020-01-12 02:45:01 +00:00
|
|
|
# State is currently being set, ignore.
|
|
|
|
if self._is_setting_light_state:
|
|
|
|
return
|
|
|
|
|
2017-07-26 07:04:40 +00:00
|
|
|
try:
|
2020-01-12 02:45:01 +00:00
|
|
|
# Update light features only once.
|
2020-04-08 19:36:45 +00:00
|
|
|
if not self._light_features:
|
|
|
|
self._light_features = self._get_light_features_retry()
|
|
|
|
self._light_state = self._get_light_state_retry()
|
2020-01-12 02:45:01 +00:00
|
|
|
self._is_available = True
|
2017-10-08 14:31:32 +00:00
|
|
|
except (SmartDeviceException, OSError) as ex:
|
2020-01-12 02:45:01 +00:00
|
|
|
if self._is_available:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning(
|
2020-01-12 02:45:01 +00:00
|
|
|
"Could not read data for %s: %s", self.smartbulb.host, ex
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-12 02:45:01 +00:00
|
|
|
self._is_available = False
|
|
|
|
|
2017-07-26 07:04:40 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2020-01-12 02:45:01 +00:00
|
|
|
return self._light_features.supported_features
|
|
|
|
|
2020-04-08 19:36:45 +00:00
|
|
|
def _get_light_features_retry(self) -> LightFeatures:
|
2020-01-12 02:45:01 +00:00
|
|
|
"""Retry the retrieval of the supported features."""
|
|
|
|
try:
|
2020-04-08 19:36:45 +00:00
|
|
|
return self._get_light_features()
|
2020-01-12 02:45:01 +00:00
|
|
|
except (SmartDeviceException, OSError):
|
|
|
|
pass
|
2017-10-19 04:52:44 +00:00
|
|
|
|
2020-01-12 02:45:01 +00:00
|
|
|
_LOGGER.debug("Retrying getting light features")
|
2020-04-08 19:36:45 +00:00
|
|
|
return self._get_light_features()
|
2020-01-12 02:45:01 +00:00
|
|
|
|
2020-04-08 19:36:45 +00:00
|
|
|
def _get_light_features(self):
|
2017-10-19 04:52:44 +00:00
|
|
|
"""Determine all supported features in one go."""
|
2020-01-12 02:45:01 +00:00
|
|
|
sysinfo = self.smartbulb.sys_info
|
|
|
|
supported_features = 0
|
2020-04-08 19:36:45 +00:00
|
|
|
# Calling api here as it reformats
|
2020-01-12 02:45:01 +00:00
|
|
|
mac = self.smartbulb.mac
|
2020-04-08 19:36:45 +00:00
|
|
|
alias = sysinfo[LIGHT_SYSINFO_ALIAS]
|
|
|
|
model = sysinfo[LIGHT_SYSINFO_MODEL]
|
2020-01-12 02:45:01 +00:00
|
|
|
min_mireds = None
|
|
|
|
max_mireds = None
|
2020-04-10 00:10:02 +00:00
|
|
|
has_emeter = self.smartbulb.has_emeter
|
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
|
|
|
|
2020-04-10 00:10:02 +00:00
|
|
|
if sysinfo.get(LIGHT_SYSINFO_IS_DIMMABLE) or LIGHT_STATE_BRIGHTNESS in sysinfo:
|
2020-01-12 02:45:01 +00:00
|
|
|
supported_features += SUPPORT_BRIGHTNESS
|
2020-04-08 19:36:45 +00:00
|
|
|
if sysinfo.get(LIGHT_SYSINFO_IS_VARIABLE_COLOR_TEMP):
|
2020-01-12 02:45:01 +00:00
|
|
|
supported_features += SUPPORT_COLOR_TEMP
|
2020-04-08 19:36:45 +00:00
|
|
|
# Have to make another api request here in
|
|
|
|
# order to not re-implement pyHS100 here
|
|
|
|
max_range, min_range = self.smartbulb.valid_temperature_range
|
|
|
|
min_mireds = kelvin_to_mired(min_range)
|
|
|
|
max_mireds = kelvin_to_mired(max_range)
|
|
|
|
if sysinfo.get(LIGHT_SYSINFO_IS_COLOR):
|
2020-01-12 02:45:01 +00:00
|
|
|
supported_features += SUPPORT_COLOR
|
|
|
|
|
|
|
|
return LightFeatures(
|
|
|
|
sysinfo=sysinfo,
|
|
|
|
mac=mac,
|
|
|
|
alias=alias,
|
|
|
|
model=model,
|
|
|
|
supported_features=supported_features,
|
|
|
|
min_mireds=min_mireds,
|
|
|
|
max_mireds=max_mireds,
|
2020-04-10 00:10:02 +00:00
|
|
|
has_emeter=has_emeter,
|
2020-01-12 02:45:01 +00:00
|
|
|
)
|
|
|
|
|
2020-04-08 19:36:45 +00:00
|
|
|
def _get_light_state_retry(self) -> LightState:
|
2020-01-12 02:45:01 +00:00
|
|
|
"""Retry the retrieval of getting light states."""
|
|
|
|
try:
|
2020-04-08 19:36:45 +00:00
|
|
|
return self._get_light_state()
|
2020-01-12 02:45:01 +00:00
|
|
|
except (SmartDeviceException, OSError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
_LOGGER.debug("Retrying getting light state")
|
2020-04-08 19:36:45 +00:00
|
|
|
return self._get_light_state()
|
2020-01-12 02:45:01 +00:00
|
|
|
|
2020-04-08 19:36:45 +00:00
|
|
|
def _light_state_from_params(self, light_state_params) -> LightState:
|
2020-01-12 02:45:01 +00:00
|
|
|
brightness = None
|
|
|
|
color_temp = None
|
|
|
|
hue_saturation = None
|
2020-04-08 19:36:45 +00:00
|
|
|
light_features = self._light_features
|
|
|
|
|
|
|
|
state = bool(light_state_params[LIGHT_STATE_ON_OFF])
|
|
|
|
|
|
|
|
if not state and LIGHT_STATE_DFT_ON in light_state_params:
|
|
|
|
light_state_params = light_state_params[LIGHT_STATE_DFT_ON]
|
2020-01-12 02:45:01 +00:00
|
|
|
|
|
|
|
if light_features.supported_features & SUPPORT_BRIGHTNESS:
|
2020-04-08 19:36:45 +00:00
|
|
|
brightness = brightness_from_percentage(
|
|
|
|
light_state_params[LIGHT_STATE_BRIGHTNESS]
|
|
|
|
)
|
2020-01-12 02:45:01 +00:00
|
|
|
|
|
|
|
if light_features.supported_features & SUPPORT_COLOR_TEMP:
|
2020-04-08 19:36:45 +00:00
|
|
|
if (
|
|
|
|
light_state_params.get(LIGHT_STATE_COLOR_TEMP) is not None
|
|
|
|
and light_state_params[LIGHT_STATE_COLOR_TEMP] != 0
|
|
|
|
):
|
|
|
|
color_temp = kelvin_to_mired(light_state_params[LIGHT_STATE_COLOR_TEMP])
|
2020-01-12 02:45:01 +00:00
|
|
|
|
|
|
|
if light_features.supported_features & SUPPORT_COLOR:
|
2020-04-08 19:36:45 +00:00
|
|
|
hue_saturation = (
|
|
|
|
light_state_params[LIGHT_STATE_HUE],
|
|
|
|
light_state_params[LIGHT_STATE_SATURATION],
|
|
|
|
)
|
|
|
|
|
|
|
|
return LightState(
|
|
|
|
state=state,
|
|
|
|
brightness=brightness,
|
|
|
|
color_temp=color_temp,
|
|
|
|
hs=hue_saturation,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _get_light_state(self) -> LightState:
|
|
|
|
"""Get the light state."""
|
|
|
|
self._update_emeter()
|
2020-04-09 22:07:39 +00:00
|
|
|
return self._light_state_from_params(self._get_device_state())
|
2020-04-08 19:36:45 +00:00
|
|
|
|
|
|
|
def _update_emeter(self):
|
2020-04-10 00:10:02 +00:00
|
|
|
if not self._light_features.has_emeter:
|
2020-04-08 19:36:45 +00:00
|
|
|
return
|
2020-01-12 02:45:01 +00:00
|
|
|
|
2020-04-08 19:36:45 +00:00
|
|
|
now = dt_util.utcnow()
|
|
|
|
if (
|
|
|
|
not self._last_current_power_update
|
|
|
|
or self._last_current_power_update + CURRENT_POWER_UPDATE_INTERVAL < now
|
|
|
|
):
|
|
|
|
self._last_current_power_update = now
|
|
|
|
self._emeter_params[ATTR_CURRENT_POWER_W] = "{:.1f}".format(
|
2020-01-12 02:45:01 +00:00
|
|
|
self.smartbulb.current_consumption()
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-04-08 19:36:45 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
not self._last_historical_power_update
|
|
|
|
or self._last_historical_power_update + HISTORICAL_POWER_UPDATE_INTERVAL
|
|
|
|
< now
|
|
|
|
):
|
|
|
|
self._last_historical_power_update = now
|
2020-01-12 02:45:01 +00:00
|
|
|
daily_statistics = self.smartbulb.get_emeter_daily()
|
|
|
|
monthly_statistics = self.smartbulb.get_emeter_monthly()
|
|
|
|
try:
|
2020-04-08 19:36:45 +00:00
|
|
|
self._emeter_params[ATTR_DAILY_ENERGY_KWH] = "{:.3f}".format(
|
2020-01-12 02:45:01 +00:00
|
|
|
daily_statistics[int(time.strftime("%d"))]
|
|
|
|
)
|
2020-04-08 19:36:45 +00:00
|
|
|
self._emeter_params[ATTR_MONTHLY_ENERGY_KWH] = "{:.3f}".format(
|
2020-01-12 02:45:01 +00:00
|
|
|
monthly_statistics[int(time.strftime("%m"))]
|
|
|
|
)
|
|
|
|
except KeyError:
|
|
|
|
# device returned no daily/monthly history
|
|
|
|
pass
|
|
|
|
|
2020-04-08 19:36:45 +00:00
|
|
|
async def _async_set_light_state_retry(
|
2020-01-12 02:45:01 +00:00
|
|
|
self, old_light_state: LightState, new_light_state: LightState
|
|
|
|
) -> None:
|
|
|
|
"""Set the light state with retry."""
|
|
|
|
# Tell the device to set the states.
|
2020-04-08 19:36:45 +00:00
|
|
|
if not _light_state_diff(old_light_state, new_light_state):
|
|
|
|
# Nothing to do, avoid the executor
|
|
|
|
return
|
|
|
|
|
2020-01-12 02:45:01 +00:00
|
|
|
self._is_setting_light_state = True
|
|
|
|
try:
|
2020-04-08 19:36:45 +00:00
|
|
|
light_state_params = await self.hass.async_add_executor_job(
|
|
|
|
self._set_light_state, old_light_state, new_light_state
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-12 02:45:01 +00:00
|
|
|
self._is_available = True
|
|
|
|
self._is_setting_light_state = False
|
2020-04-08 19:36:45 +00:00
|
|
|
if LIGHT_STATE_ERROR_MSG in light_state_params:
|
|
|
|
raise HomeAssistantError(light_state_params[LIGHT_STATE_ERROR_MSG])
|
|
|
|
self._light_state = self._light_state_from_params(light_state_params)
|
2020-01-12 02:45:01 +00:00
|
|
|
return
|
|
|
|
except (SmartDeviceException, OSError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
_LOGGER.debug("Retrying setting light state")
|
2020-04-08 19:36:45 +00:00
|
|
|
light_state_params = await self.hass.async_add_executor_job(
|
|
|
|
self._set_light_state, old_light_state, new_light_state
|
2020-01-12 02:45:01 +00:00
|
|
|
)
|
|
|
|
self._is_available = True
|
2020-04-08 19:36:45 +00:00
|
|
|
if LIGHT_STATE_ERROR_MSG in light_state_params:
|
|
|
|
raise HomeAssistantError(light_state_params[LIGHT_STATE_ERROR_MSG])
|
|
|
|
self._light_state = self._light_state_from_params(light_state_params)
|
2020-01-12 02:45:01 +00:00
|
|
|
except (SmartDeviceException, OSError) as ex:
|
|
|
|
self._is_available = False
|
|
|
|
_LOGGER.warning("Could not set data for %s: %s", self.smartbulb.host, ex)
|
|
|
|
|
|
|
|
self._is_setting_light_state = False
|
|
|
|
|
2020-04-08 19:36:45 +00:00
|
|
|
def _set_light_state(
|
2020-01-12 02:45:01 +00:00
|
|
|
self, old_light_state: LightState, new_light_state: LightState
|
|
|
|
) -> None:
|
|
|
|
"""Set the light state."""
|
2020-04-08 19:36:45 +00:00
|
|
|
diff = _light_state_diff(old_light_state, new_light_state)
|
|
|
|
|
|
|
|
if not diff:
|
|
|
|
return
|
|
|
|
|
2020-04-09 22:07:39 +00:00
|
|
|
return self._set_device_state(diff)
|
|
|
|
|
|
|
|
def _get_device_state(self):
|
|
|
|
"""State of the bulb or smart dimmer switch."""
|
|
|
|
if isinstance(self.smartbulb, SmartBulb):
|
|
|
|
return self.smartbulb.get_light_state()
|
|
|
|
|
2020-04-10 00:10:02 +00:00
|
|
|
sysinfo = self.smartbulb.sys_info
|
2020-04-09 22:07:39 +00:00
|
|
|
# Its not really a bulb, its a dimmable SmartPlug (aka Wall Switch)
|
|
|
|
return {
|
2020-04-10 00:10:02 +00:00
|
|
|
LIGHT_STATE_ON_OFF: sysinfo[LIGHT_STATE_RELAY_STATE],
|
|
|
|
LIGHT_STATE_BRIGHTNESS: sysinfo.get(LIGHT_STATE_BRIGHTNESS, 0),
|
2020-04-09 22:07:39 +00:00
|
|
|
LIGHT_STATE_COLOR_TEMP: 0,
|
|
|
|
LIGHT_STATE_HUE: 0,
|
|
|
|
LIGHT_STATE_SATURATION: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
def _set_device_state(self, state):
|
|
|
|
"""Set state of the bulb or smart dimmer switch."""
|
|
|
|
if isinstance(self.smartbulb, SmartBulb):
|
|
|
|
return self.smartbulb.set_light_state(state)
|
|
|
|
|
|
|
|
# Its not really a bulb, its a dimmable SmartPlug (aka Wall Switch)
|
|
|
|
if LIGHT_STATE_BRIGHTNESS in state:
|
|
|
|
# Brightness of 0 is accepted by the
|
|
|
|
# device but the underlying library rejects it
|
|
|
|
# so we turn off instead.
|
|
|
|
if state[LIGHT_STATE_BRIGHTNESS]:
|
|
|
|
self.smartbulb.brightness = state[LIGHT_STATE_BRIGHTNESS]
|
|
|
|
else:
|
2020-04-10 00:10:02 +00:00
|
|
|
self.smartbulb.state = self.smartbulb.SWITCH_STATE_OFF
|
2020-04-09 22:07:39 +00:00
|
|
|
elif LIGHT_STATE_ON_OFF in state:
|
2020-04-10 00:10:02 +00:00
|
|
|
if state[LIGHT_STATE_ON_OFF]:
|
|
|
|
self.smartbulb.state = self.smartbulb.SWITCH_STATE_ON
|
|
|
|
else:
|
|
|
|
self.smartbulb.state = self.smartbulb.SWITCH_STATE_OFF
|
2020-04-09 22:07:39 +00:00
|
|
|
|
|
|
|
return self._get_device_state()
|
2020-04-08 19:36:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _light_state_diff(old_light_state: LightState, new_light_state: LightState):
|
|
|
|
old_state_param = old_light_state.to_param()
|
|
|
|
new_state_param = new_light_state.to_param()
|
|
|
|
|
|
|
|
return {
|
|
|
|
key: value
|
|
|
|
for key, value in new_state_param.items()
|
|
|
|
if new_state_param.get(key) != old_state_param.get(key)
|
|
|
|
}
|