core/homeassistant/components/ihc/light.py

137 lines
4.3 KiB
Python
Raw Normal View History

"""Support for IHC lights."""
from __future__ import annotations
from ihcsdk.ihccontroller import IHCController
2020-04-26 16:49:41 +00:00
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
SUPPORT_BRIGHTNESS,
LightEntity,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import CONF_DIMMABLE, CONF_OFF_ID, CONF_ON_ID, DOMAIN, IHC_CONTROLLER
from .ihcdevice import IHCDevice
from .util import async_pulse, async_set_bool, async_set_int
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
2018-08-19 20:29:08 +00:00
"""Set up the IHC lights platform."""
Add support for multiple IHC controllers (#18058) * Added support for secondary IHC controller Most IHC systems only have one controller but the system can be setup with a linked secondary controller. I have updated the code to have it support both primary and secondary controller. Existing configuration is not impacted and secondary controller can be setup the same way, with similar settings nested under 'secondary' in the configuration * Update __init__.py * Update __init__.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update __init__.py * Update const.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update __init__.py * Update const.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update ihc.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py indentation was incorrect for "load_platform" in "get_manual_configuration". Load_platform was not called with the correct component name
2018-11-22 08:45:40 +00:00
if discovery_info is None:
return
devices = []
Add support for multiple IHC controllers (#18058) * Added support for secondary IHC controller Most IHC systems only have one controller but the system can be setup with a linked secondary controller. I have updated the code to have it support both primary and secondary controller. Existing configuration is not impacted and secondary controller can be setup the same way, with similar settings nested under 'secondary' in the configuration * Update __init__.py * Update __init__.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update __init__.py * Update const.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update __init__.py * Update const.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update ihc.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py indentation was incorrect for "load_platform" in "get_manual_configuration". Load_platform was not called with the correct component name
2018-11-22 08:45:40 +00:00
for name, device in discovery_info.items():
2019-07-31 19:25:30 +00:00
ihc_id = device["ihc_id"]
product_cfg = device["product_cfg"]
product = device["product"]
Add support for multiple IHC controllers (#18058) * Added support for secondary IHC controller Most IHC systems only have one controller but the system can be setup with a linked secondary controller. I have updated the code to have it support both primary and secondary controller. Existing configuration is not impacted and secondary controller can be setup the same way, with similar settings nested under 'secondary' in the configuration * Update __init__.py * Update __init__.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update __init__.py * Update const.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update __init__.py * Update const.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update ihc.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py indentation was incorrect for "load_platform" in "get_manual_configuration". Load_platform was not called with the correct component name
2018-11-22 08:45:40 +00:00
# Find controller that corresponds with device id
controller_id = device["ctrl_id"]
ihc_controller: IHCController = hass.data[DOMAIN][controller_id][IHC_CONTROLLER]
ihc_off_id = product_cfg.get(CONF_OFF_ID)
ihc_on_id = product_cfg.get(CONF_ON_ID)
Add support for multiple IHC controllers (#18058) * Added support for secondary IHC controller Most IHC systems only have one controller but the system can be setup with a linked secondary controller. I have updated the code to have it support both primary and secondary controller. Existing configuration is not impacted and secondary controller can be setup the same way, with similar settings nested under 'secondary' in the configuration * Update __init__.py * Update __init__.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update __init__.py * Update const.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update __init__.py * Update const.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update ihc.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py indentation was incorrect for "load_platform" in "get_manual_configuration". Load_platform was not called with the correct component name
2018-11-22 08:45:40 +00:00
dimmable = product_cfg[CONF_DIMMABLE]
2019-07-31 19:25:30 +00:00
light = IhcLight(
ihc_controller,
controller_id,
name,
ihc_id,
ihc_off_id,
ihc_on_id,
dimmable,
product,
2019-07-31 19:25:30 +00:00
)
Add support for multiple IHC controllers (#18058) * Added support for secondary IHC controller Most IHC systems only have one controller but the system can be setup with a linked secondary controller. I have updated the code to have it support both primary and secondary controller. Existing configuration is not impacted and secondary controller can be setup the same way, with similar settings nested under 'secondary' in the configuration * Update __init__.py * Update __init__.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update __init__.py * Update const.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update __init__.py * Update const.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update __init__.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update ihc.py * Update __init__.py * Update __init__.py * Update ihc.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py * Update __init__.py indentation was incorrect for "load_platform" in "get_manual_configuration". Load_platform was not called with the correct component name
2018-11-22 08:45:40 +00:00
devices.append(light)
add_entities(devices)
2020-04-26 16:49:41 +00:00
class IhcLight(IHCDevice, LightEntity):
"""Representation of a IHC light.
For dimmable lights, the associated IHC resource should be a light
level (integer). For non dimmable light the IHC resource should be
an on/off (boolean) resource
"""
2019-07-31 19:25:30 +00:00
def __init__(
self,
ihc_controller: IHCController,
controller_id: str,
name: str,
2019-07-31 19:25:30 +00:00
ihc_id: int,
ihc_off_id: int,
ihc_on_id: int,
dimmable=False,
product=None,
) -> None:
"""Initialize the light."""
super().__init__(ihc_controller, controller_id, name, ihc_id, product)
self._ihc_off_id = ihc_off_id
self._ihc_on_id = ihc_on_id
self._brightness = 0
self._dimmable = dimmable
2021-05-01 19:01:56 +00:00
self._state = False
@property
def brightness(self) -> int:
"""Return the brightness of this light between 0..255."""
return self._brightness
@property
def is_on(self) -> bool:
"""Return true if light is on."""
return self._state
@property
def supported_features(self):
"""Flag supported features."""
if self._dimmable:
return SUPPORT_BRIGHTNESS
return 0
async def async_turn_on(self, **kwargs):
"""Turn the light on."""
if ATTR_BRIGHTNESS in kwargs:
brightness = kwargs[ATTR_BRIGHTNESS]
else:
2021-10-17 18:24:34 +00:00
if (brightness := self._brightness) == 0:
brightness = 255
if self._dimmable:
2019-07-31 19:25:30 +00:00
await async_set_int(
self.hass, self.ihc_controller, self.ihc_id, int(brightness * 100 / 255)
)
else:
if self._ihc_on_id:
2019-07-31 19:25:30 +00:00
await async_pulse(self.hass, self.ihc_controller, self._ihc_on_id)
else:
2019-07-31 19:25:30 +00:00
await async_set_bool(self.hass, self.ihc_controller, self.ihc_id, True)
async def async_turn_off(self, **kwargs):
"""Turn the light off."""
if self._dimmable:
2019-07-31 19:25:30 +00:00
await async_set_int(self.hass, self.ihc_controller, self.ihc_id, 0)
else:
if self._ihc_off_id:
2019-07-31 19:25:30 +00:00
await async_pulse(self.hass, self.ihc_controller, self._ihc_off_id)
else:
2019-07-31 19:25:30 +00:00
await async_set_bool(self.hass, self.ihc_controller, self.ihc_id, False)
def on_ihc_change(self, ihc_id, value):
"""Handle IHC notifications."""
if isinstance(value, bool):
self._dimmable = False
self._state = value != 0
else:
self._dimmable = True
self._state = value > 0
if self._state:
self._brightness = int(value * 255 / 100)
self.schedule_update_ha_state()