mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Add emeter attributes (#9903)
* Add emeter attributes. * Remove unused attributes. * Rework supported features so it only queries the bulb once. * Used cached supported_features, catch errors if energy usage not reported.
This commit is contained in:
parent
429b637885
commit
6ea866c7f7
@ -6,6 +6,8 @@ https://home-assistant.io/components/light.tplink/
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import colorsys
|
import colorsys
|
||||||
|
import time
|
||||||
|
|
||||||
from homeassistant.const import (CONF_HOST, CONF_NAME)
|
from homeassistant.const import (CONF_HOST, CONF_NAME)
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
Light, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_KELVIN, ATTR_RGB_COLOR,
|
Light, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_KELVIN, ATTR_RGB_COLOR,
|
||||||
@ -21,7 +23,9 @@ REQUIREMENTS = ['pyHS100==0.3.0']
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
SUPPORT_TPLINK = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP)
|
ATTR_CURRENT_CONSUMPTION = 'current_consumption'
|
||||||
|
ATTR_DAILY_CONSUMPTION = 'daily_consumption'
|
||||||
|
ATTR_MONTHLY_CONSUMPTION = 'monthly_consumption'
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
@ -64,24 +68,26 @@ class TPLinkSmartBulb(Light):
|
|||||||
def __init__(self, smartbulb: 'SmartBulb', name):
|
def __init__(self, smartbulb: 'SmartBulb', name):
|
||||||
"""Initialize the bulb."""
|
"""Initialize the bulb."""
|
||||||
self.smartbulb = smartbulb
|
self.smartbulb = smartbulb
|
||||||
|
self._name = None
|
||||||
# Use the name set on the device if not set
|
if name is not None:
|
||||||
if name is None:
|
|
||||||
self._name = self.smartbulb.alias
|
|
||||||
else:
|
|
||||||
self._name = name
|
self._name = name
|
||||||
|
|
||||||
self._state = None
|
self._state = None
|
||||||
self._color_temp = None
|
self._color_temp = None
|
||||||
self._brightness = None
|
self._brightness = None
|
||||||
self._rgb = None
|
self._rgb = None
|
||||||
_LOGGER.debug("Setting up TP-Link Smart Bulb")
|
self._supported_features = 0
|
||||||
|
self._emeter_params = {}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the Smart Bulb, if any."""
|
"""Return the name of the Smart Bulb, if any."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_state_attributes(self):
|
||||||
|
"""Return the state attributes of the device."""
|
||||||
|
return self._emeter_params
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the light on."""
|
"""Turn the light on."""
|
||||||
self.smartbulb.state = self.smartbulb.BULB_STATE_ON
|
self.smartbulb.state = self.smartbulb.BULB_STATE_ON
|
||||||
@ -119,30 +125,57 @@ class TPLinkSmartBulb(Light):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""True if device is on."""
|
"""Return True if device is on."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the TP-Link Bulb's state."""
|
"""Update the TP-Link Bulb's state."""
|
||||||
from pyHS100 import SmartDeviceException
|
from pyHS100 import SmartDeviceException
|
||||||
try:
|
try:
|
||||||
|
if self._supported_features == 0:
|
||||||
|
self.get_features()
|
||||||
self._state = (
|
self._state = (
|
||||||
self.smartbulb.state == self.smartbulb.BULB_STATE_ON)
|
self.smartbulb.state == self.smartbulb.BULB_STATE_ON)
|
||||||
|
if self._name is None:
|
||||||
|
self._name = self.smartbulb.alias
|
||||||
|
if self._supported_features & SUPPORT_BRIGHTNESS:
|
||||||
self._brightness = brightness_from_percentage(
|
self._brightness = brightness_from_percentage(
|
||||||
self.smartbulb.brightness)
|
self.smartbulb.brightness)
|
||||||
if self.smartbulb.is_color:
|
if self._supported_features & SUPPORT_COLOR_TEMP:
|
||||||
if (self.smartbulb.color_temp is not None and
|
if (self.smartbulb.color_temp is not None and
|
||||||
self.smartbulb.color_temp != 0):
|
self.smartbulb.color_temp != 0):
|
||||||
self._color_temp = kelvin_to_mired(
|
self._color_temp = kelvin_to_mired(
|
||||||
self.smartbulb.color_temp)
|
self.smartbulb.color_temp)
|
||||||
|
if self._supported_features & SUPPORT_RGB_COLOR:
|
||||||
self._rgb = hsv_to_rgb(self.smartbulb.hsv)
|
self._rgb = hsv_to_rgb(self.smartbulb.hsv)
|
||||||
|
if self.smartbulb.has_emeter:
|
||||||
|
self._emeter_params[ATTR_CURRENT_CONSUMPTION] \
|
||||||
|
= "%.1f W" % self.smartbulb.current_consumption()
|
||||||
|
daily_statistics = self.smartbulb.get_emeter_daily()
|
||||||
|
monthly_statistics = self.smartbulb.get_emeter_monthly()
|
||||||
|
try:
|
||||||
|
self._emeter_params[ATTR_DAILY_CONSUMPTION] \
|
||||||
|
= "%.2f kW" % daily_statistics[int(
|
||||||
|
time.strftime("%d"))]
|
||||||
|
self._emeter_params[ATTR_MONTHLY_CONSUMPTION] \
|
||||||
|
= "%.2f kW" % monthly_statistics[int(
|
||||||
|
time.strftime("%m"))]
|
||||||
|
except KeyError:
|
||||||
|
# device returned no daily/monthly history
|
||||||
|
pass
|
||||||
except (SmartDeviceException, OSError) as ex:
|
except (SmartDeviceException, OSError) as ex:
|
||||||
_LOGGER.warning('Could not read state for %s: %s', self.name, ex)
|
_LOGGER.warning('Could not read state for %s: %s', self._name, ex)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
supported_features = SUPPORT_TPLINK
|
return self._supported_features
|
||||||
|
|
||||||
|
def get_features(self):
|
||||||
|
"""Determine all supported features in one go."""
|
||||||
|
if self.smartbulb.is_dimmable:
|
||||||
|
self._supported_features += SUPPORT_BRIGHTNESS
|
||||||
|
if self.smartbulb.is_variable_color_temp:
|
||||||
|
self._supported_features += SUPPORT_COLOR_TEMP
|
||||||
if self.smartbulb.is_color:
|
if self.smartbulb.is_color:
|
||||||
supported_features += SUPPORT_RGB_COLOR
|
self._supported_features += SUPPORT_RGB_COLOR
|
||||||
return supported_features
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user