2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Avion dimmers."""
|
2019-03-17 03:44:05 +00:00
|
|
|
import importlib
|
2017-07-04 22:39:51 +00:00
|
|
|
import time
|
2017-01-19 20:07:37 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-29 22:29:27 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_DEVICES,
|
|
|
|
CONF_ID,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2017-01-19 20:07:37 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2018-10-29 22:29:27 +00:00
|
|
|
SUPPORT_AVION_LED = SUPPORT_BRIGHTNESS
|
2017-01-19 20:07:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Optional(CONF_ID): cv.positive_int,
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2017-01-19 20:07:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_DEVICES, default={}): {cv.string: DEVICE_SCHEMA},
|
|
|
|
vol.Optional(CONF_USERNAME): cv.string,
|
|
|
|
vol.Optional(CONF_PASSWORD): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2017-01-19 20:07:37 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-01-19 20:07:37 +00:00
|
|
|
"""Set up an Avion switch."""
|
2019-07-31 19:25:30 +00:00
|
|
|
avion = importlib.import_module("avion")
|
2017-07-04 22:39:51 +00:00
|
|
|
|
2017-01-19 20:07:37 +00:00
|
|
|
lights = []
|
2017-07-04 22:39:51 +00:00
|
|
|
if CONF_USERNAME in config and CONF_PASSWORD in config:
|
2019-07-31 19:25:30 +00:00
|
|
|
devices = avion.get_devices(config[CONF_USERNAME], config[CONF_PASSWORD])
|
2018-10-29 22:29:27 +00:00
|
|
|
for device in devices:
|
|
|
|
lights.append(AvionLight(device))
|
2017-07-04 22:39:51 +00:00
|
|
|
|
2017-01-19 20:07:37 +00:00
|
|
|
for address, device_config in config[CONF_DEVICES].items():
|
2018-10-29 22:29:27 +00:00
|
|
|
device = avion.Avion(
|
|
|
|
mac=address,
|
|
|
|
passphrase=device_config[CONF_API_KEY],
|
|
|
|
name=device_config.get(CONF_NAME),
|
|
|
|
object_id=device_config.get(CONF_ID),
|
2019-07-31 19:25:30 +00:00
|
|
|
connect=False,
|
|
|
|
)
|
2017-07-04 22:39:51 +00:00
|
|
|
lights.append(AvionLight(device))
|
2017-01-19 20:07:37 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(lights)
|
2017-01-19 20:07:37 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class AvionLight(LightEntity):
|
2017-01-19 20:07:37 +00:00
|
|
|
"""Representation of an Avion light."""
|
|
|
|
|
2021-07-08 07:16:47 +00:00
|
|
|
_attr_supported_features = SUPPORT_AVION_LED
|
|
|
|
_attr_should_poll = False
|
|
|
|
_attr_assumed_state = True
|
|
|
|
|
2017-01-19 20:07:37 +00:00
|
|
|
def __init__(self, device):
|
|
|
|
"""Initialize the light."""
|
2021-07-08 07:16:47 +00:00
|
|
|
self._attr_name = device.name
|
|
|
|
self._attr_unique_id = device.mac
|
|
|
|
self._attr_brightness = 255
|
2018-10-29 22:29:27 +00:00
|
|
|
self._switch = device
|
2017-01-19 20:07:37 +00:00
|
|
|
|
|
|
|
def set_state(self, brightness):
|
|
|
|
"""Set the state of this lamp to the provided brightness."""
|
2019-07-31 19:25:30 +00:00
|
|
|
avion = importlib.import_module("avion")
|
2017-07-07 06:20:39 +00:00
|
|
|
|
2017-07-04 22:39:51 +00:00
|
|
|
# Bluetooth LE is unreliable, and the connection may drop at any
|
|
|
|
# time. Make an effort to re-establish the link.
|
|
|
|
initial = time.monotonic()
|
|
|
|
while True:
|
|
|
|
if time.monotonic() - initial >= 10:
|
|
|
|
return False
|
|
|
|
try:
|
|
|
|
self._switch.set_brightness(brightness)
|
|
|
|
break
|
2018-10-29 22:29:27 +00:00
|
|
|
except avion.AvionException:
|
2017-07-04 22:39:51 +00:00
|
|
|
self._switch.connect()
|
2017-01-19 20:07:37 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the specified or all lights on."""
|
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
|
|
|
|
|
|
|
if brightness is not None:
|
2021-07-08 07:16:47 +00:00
|
|
|
self._attr_brightness = brightness
|
2017-01-19 20:07:37 +00:00
|
|
|
|
|
|
|
self.set_state(self.brightness)
|
2021-07-08 07:16:47 +00:00
|
|
|
self._attr_is_on = True
|
2017-01-19 20:07:37 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the specified or all lights off."""
|
|
|
|
self.set_state(0)
|
2021-07-08 07:16:47 +00:00
|
|
|
self._attr_is_on = False
|