core/homeassistant/components/homematicip_cloud/light.py

120 lines
3.8 KiB
Python
Raw Normal View History

"""
2018-08-21 19:25:16 +00:00
Support for HomematicIP Cloud lights.
2018-08-21 19:25:16 +00:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.homematicip_cloud/
"""
import logging
from homeassistant.components.homematicip_cloud import (
2018-08-21 19:25:16 +00:00
HMIPC_HAPID, HomematicipGenericDevice)
from homeassistant.components.homematicip_cloud import DOMAIN as HMIPC_DOMAIN
from homeassistant.components.light import (
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light)
DEPENDENCIES = ['homematicip_cloud']
_LOGGER = logging.getLogger(__name__)
2018-08-21 19:25:16 +00:00
ATTR_ENERGY_COUNTER = 'energy_counter_kwh'
ATTR_POWER_CONSUMPTION = 'power_consumption'
ATTR_PROFILE_MODE = 'profile_mode'
2018-08-21 19:25:16 +00:00
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None):
2018-08-21 19:25:16 +00:00
"""Old way of setting up HomematicIP Cloud lights."""
Add HomematicIP Cloud Config Flow and Entries loading (#14861) * Add HomematicIP Cloud to config flow * Inititial trial for config_flow * Integrations text files * Load and write config_flow and init homematicip_cloud * Split into dedicated files * Ceanup of text messages * Working config_flow * Move imports inside a function * Enable laoding even no accesspoints are defined * Revert unnecassary changes in CONFIG_SCHEMA * Better error handling * fix flask8 * Migration to async for token generation * A few fixes * Simplify config_flow * Bump version to 9.6 with renamed package * Requirements file * First fixes after review * Implement async_step_import * Cleanup for Config Flow * First tests for homematicip_cloud setup * Remove config_flow tests * Really remove all things * Fix comment * Update picture * Add support for async_setup_entry to switch and climate platform * Update path of the config_flow picture * Refactoring for better tesability * Further tests implemented * Move 3th party lib inside function * Fix lint * Update requirments_test_all.txt file * UPdate of requirments_test_all.txt did not work * Furder cleanup in websocket connection * Remove a test for the hap * Revert "Remove a test for the hap" This reverts commit 968d58cba108e0f371022c7ab540374aa2ab13f4. * First tests implemented for config_flow * Fix lint * Rework of client registration process * Implemented tests for config_flow 100% coverage * Cleanup * Cleanup comments and code * Try to fix import problem * Add homematicip to the test env requirements
2018-07-06 21:05:34 +00:00
pass
async def async_setup_entry(hass, config_entry, async_add_entities):
2018-08-21 19:25:16 +00:00
"""Set up the HomematicIP Cloud lights from a config entry."""
from homematicip.aio.device import AsyncBrandSwitchMeasuring, AsyncDimmer,\
AsyncPluggableDimmer, AsyncBrandDimmer, AsyncFullFlushDimmer
Add HomematicIP Cloud Config Flow and Entries loading (#14861) * Add HomematicIP Cloud to config flow * Inititial trial for config_flow * Integrations text files * Load and write config_flow and init homematicip_cloud * Split into dedicated files * Ceanup of text messages * Working config_flow * Move imports inside a function * Enable laoding even no accesspoints are defined * Revert unnecassary changes in CONFIG_SCHEMA * Better error handling * fix flask8 * Migration to async for token generation * A few fixes * Simplify config_flow * Bump version to 9.6 with renamed package * Requirements file * First fixes after review * Implement async_step_import * Cleanup for Config Flow * First tests for homematicip_cloud setup * Remove config_flow tests * Really remove all things * Fix comment * Update picture * Add support for async_setup_entry to switch and climate platform * Update path of the config_flow picture * Refactoring for better tesability * Further tests implemented * Move 3th party lib inside function * Fix lint * Update requirments_test_all.txt file * UPdate of requirments_test_all.txt did not work * Furder cleanup in websocket connection * Remove a test for the hap * Revert "Remove a test for the hap" This reverts commit 968d58cba108e0f371022c7ab540374aa2ab13f4. * First tests implemented for config_flow * Fix lint * Rework of client registration process * Implemented tests for config_flow 100% coverage * Cleanup * Cleanup comments and code * Try to fix import problem * Add homematicip to the test env requirements
2018-07-06 21:05:34 +00:00
home = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]].home
devices = []
for device in home.devices:
if isinstance(device, AsyncBrandSwitchMeasuring):
devices.append(HomematicipLightMeasuring(home, device))
elif isinstance(device,
(AsyncDimmer, AsyncPluggableDimmer,
AsyncBrandDimmer, AsyncFullFlushDimmer)):
devices.append(HomematicipDimmer(home, device))
if devices:
async_add_entities(devices)
class HomematicipLight(HomematicipGenericDevice, Light):
2018-08-21 19:25:16 +00:00
"""Representation of a HomematicIP Cloud light device."""
def __init__(self, home, device):
"""Initialize the light device."""
super().__init__(home, device)
@property
def is_on(self):
"""Return true if device is on."""
return self._device.on
async def async_turn_on(self, **kwargs):
"""Turn the device on."""
await self._device.turn_on()
async def async_turn_off(self, **kwargs):
"""Turn the device off."""
await self._device.turn_off()
class HomematicipLightMeasuring(HomematicipLight):
2018-08-21 19:25:16 +00:00
"""Representation of a HomematicIP Cloud measuring light device."""
@property
def device_state_attributes(self):
"""Return the state attributes of the generic device."""
attr = super().device_state_attributes
if self._device.currentPowerConsumption > 0.05:
attr.update({
ATTR_POWER_CONSUMPTION:
round(self._device.currentPowerConsumption, 2)
})
attr.update({
2018-08-21 19:25:16 +00:00
ATTR_ENERGY_COUNTER: round(self._device.energyCounter, 2)
})
return attr
class HomematicipDimmer(HomematicipGenericDevice, Light):
2018-08-21 19:25:16 +00:00
"""Representation of HomematicIP Cloud dimmer light device."""
def __init__(self, home, device):
"""Initialize the dimmer light device."""
super().__init__(home, device)
@property
def is_on(self):
"""Return true if device is on."""
return self._device.dimLevel != 0
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
return int(self._device.dimLevel*255)
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_BRIGHTNESS
async def async_turn_on(self, **kwargs):
"""Turn the light on."""
if ATTR_BRIGHTNESS in kwargs:
2018-08-21 19:25:16 +00:00
await self._device.set_dim_level(kwargs[ATTR_BRIGHTNESS]/255.0)
else:
await self._device.set_dim_level(1)
async def async_turn_off(self, **kwargs):
"""Turn the light off."""
await self._device.set_dim_level(0)