2019-02-28 13:16:38 +01:00
|
|
|
"""Support for myStrom Wifi bulbs."""
|
2017-04-18 18:03:56 +02:00
|
|
|
import logging
|
|
|
|
|
2019-12-04 00:44:50 +01:00
|
|
|
from pymystrom.bulb import MyStromBulb
|
|
|
|
from pymystrom.exceptions import MyStromConnectionError
|
2017-04-18 18:03:56 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.light import (
|
2019-07-31 21:25:30 +02:00
|
|
|
ATTR_BRIGHTNESS,
|
2019-12-04 00:44:50 +01:00
|
|
|
ATTR_EFFECT,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
PLATFORM_SCHEMA,
|
2019-07-31 21:25:30 +02:00
|
|
|
SUPPORT_BRIGHTNESS,
|
2019-12-04 00:44:50 +01:00
|
|
|
SUPPORT_COLOR,
|
2019-07-31 21:25:30 +02:00
|
|
|
SUPPORT_EFFECT,
|
|
|
|
SUPPORT_FLASH,
|
2020-04-26 18:49:41 +02:00
|
|
|
LightEntity,
|
2019-07-31 21:25:30 +02:00
|
|
|
)
|
2018-06-10 11:38:23 +02:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME
|
2020-04-24 01:00:17 +02:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2019-12-04 00:44:50 +01:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-04-18 18:03:56 +02:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
DEFAULT_NAME = "myStrom bulb"
|
2017-04-18 18:03:56 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
SUPPORT_MYSTROM = SUPPORT_BRIGHTNESS | SUPPORT_EFFECT | SUPPORT_FLASH | SUPPORT_COLOR
|
2017-07-11 21:22:00 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
EFFECT_RAINBOW = "rainbow"
|
|
|
|
EFFECT_SUNRISE = "sunrise"
|
2017-07-11 21:22:00 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
MYSTROM_EFFECT_LIST = [EFFECT_RAINBOW, EFFECT_SUNRISE]
|
2017-04-18 18:03:56 +02:00
|
|
|
|
2019-07-31 21:25:30 +02:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_MAC): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2017-04-18 18:03:56 +02:00
|
|
|
|
|
|
|
|
2020-04-24 01:00:17 +02:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
|
|
"""Set up the myStrom light integration."""
|
2017-04-18 18:03:56 +02:00
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
mac = config.get(CONF_MAC)
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
|
|
|
|
bulb = MyStromBulb(host, mac)
|
|
|
|
try:
|
2020-04-24 01:00:17 +02:00
|
|
|
await bulb.get_state()
|
|
|
|
if bulb.bulb_type != "rgblamp":
|
2017-04-18 18:03:56 +02:00
|
|
|
_LOGGER.error("Device %s (%s) is not a myStrom bulb", host, mac)
|
2018-06-10 11:38:23 +02:00
|
|
|
return
|
2017-04-18 18:03:56 +02:00
|
|
|
except MyStromConnectionError:
|
2020-04-24 01:00:17 +02:00
|
|
|
_LOGGER.warning("No route to myStrom bulb: %s", host)
|
|
|
|
raise PlatformNotReady()
|
2017-04-18 18:03:56 +02:00
|
|
|
|
2020-04-24 01:00:17 +02:00
|
|
|
async_add_entities([MyStromLight(bulb, name, mac)], True)
|
2017-04-18 18:03:56 +02:00
|
|
|
|
|
|
|
|
2020-04-26 18:49:41 +02:00
|
|
|
class MyStromLight(LightEntity):
|
2020-04-24 01:00:17 +02:00
|
|
|
"""Representation of the myStrom WiFi bulb."""
|
2017-04-18 18:03:56 +02:00
|
|
|
|
2020-04-24 01:00:17 +02:00
|
|
|
def __init__(self, bulb, name, mac):
|
2017-04-18 18:03:56 +02:00
|
|
|
"""Initialize the light."""
|
|
|
|
self._bulb = bulb
|
|
|
|
self._name = name
|
|
|
|
self._state = None
|
|
|
|
self._available = False
|
|
|
|
self._brightness = 0
|
2018-03-15 16:45:27 +01:00
|
|
|
self._color_h = 0
|
|
|
|
self._color_s = 0
|
2020-04-24 01:00:17 +02:00
|
|
|
self._mac = mac
|
2017-04-18 18:03:56 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the display name of this light."""
|
|
|
|
return self._name
|
|
|
|
|
2020-04-24 01:00:17 +02:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._mac
|
|
|
|
|
2017-04-18 18:03:56 +02:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_MYSTROM
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""Return the brightness of the light."""
|
2017-04-18 18:03:56 +02:00
|
|
|
return self._brightness
|
|
|
|
|
2018-03-15 16:45:27 +01:00
|
|
|
@property
|
2018-03-18 23:00:29 +01:00
|
|
|
def hs_color(self):
|
2018-03-15 16:45:27 +01:00
|
|
|
"""Return the color of the light."""
|
2018-03-18 23:00:29 +01:00
|
|
|
return self._color_h, self._color_s
|
2018-03-15 16:45:27 +01:00
|
|
|
|
2017-04-18 18:03:56 +02:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self._available
|
|
|
|
|
2017-07-11 21:22:00 +02:00
|
|
|
@property
|
|
|
|
def effect_list(self):
|
|
|
|
"""Return the list of supported effects."""
|
|
|
|
return MYSTROM_EFFECT_LIST
|
|
|
|
|
2017-04-18 18:03:56 +02:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
2020-04-24 01:00:17 +02:00
|
|
|
return self._state
|
2017-04-18 18:03:56 +02:00
|
|
|
|
2020-04-24 01:00:17 +02:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2017-04-18 18:03:56 +02:00
|
|
|
"""Turn on the light."""
|
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
|
2017-07-11 21:22:00 +02:00
|
|
|
effect = kwargs.get(ATTR_EFFECT)
|
2017-04-18 18:03:56 +02:00
|
|
|
|
2018-03-18 23:00:29 +01:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
|
|
|
color_h, color_s = kwargs[ATTR_HS_COLOR]
|
2018-03-15 16:45:27 +01:00
|
|
|
elif ATTR_BRIGHTNESS in kwargs:
|
|
|
|
# Brightness update, keep color
|
|
|
|
color_h, color_s = self._color_h, self._color_s
|
|
|
|
else:
|
|
|
|
color_h, color_s = 0, 0 # Back to white
|
|
|
|
|
2017-04-18 18:03:56 +02:00
|
|
|
try:
|
|
|
|
if not self.is_on:
|
2020-04-24 01:00:17 +02:00
|
|
|
await self._bulb.set_on()
|
2017-04-18 18:03:56 +02:00
|
|
|
if brightness is not None:
|
2020-04-24 01:00:17 +02:00
|
|
|
await self._bulb.set_color_hsv(
|
2018-03-15 16:45:27 +01:00
|
|
|
int(color_h), int(color_s), round(brightness * 100 / 255)
|
|
|
|
)
|
2017-07-11 21:22:00 +02:00
|
|
|
if effect == EFFECT_SUNRISE:
|
2020-04-24 01:00:17 +02:00
|
|
|
await self._bulb.set_sunrise(30)
|
2017-07-11 21:22:00 +02:00
|
|
|
if effect == EFFECT_RAINBOW:
|
2020-04-24 01:00:17 +02:00
|
|
|
await self._bulb.set_rainbow(30)
|
2017-04-18 18:03:56 +02:00
|
|
|
except MyStromConnectionError:
|
2020-04-24 01:00:17 +02:00
|
|
|
_LOGGER.warning("No route to myStrom bulb")
|
2017-04-18 18:03:56 +02:00
|
|
|
|
2020-04-24 01:00:17 +02:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-04-18 18:03:56 +02:00
|
|
|
"""Turn off the bulb."""
|
|
|
|
try:
|
2020-04-24 01:00:17 +02:00
|
|
|
await self._bulb.set_off()
|
2017-04-18 18:03:56 +02:00
|
|
|
except MyStromConnectionError:
|
|
|
|
_LOGGER.warning("myStrom bulb not online")
|
|
|
|
|
2020-04-24 01:00:17 +02:00
|
|
|
async def async_update(self):
|
2017-04-18 18:03:56 +02:00
|
|
|
"""Fetch new state data for this light."""
|
|
|
|
try:
|
2020-04-24 01:00:17 +02:00
|
|
|
await self._bulb.get_state()
|
|
|
|
self._state = self._bulb.state
|
2018-03-15 16:45:27 +01:00
|
|
|
|
2020-04-24 01:00:17 +02:00
|
|
|
colors = self._bulb.color
|
2018-07-13 20:01:57 +02:00
|
|
|
try:
|
2019-07-31 21:25:30 +02:00
|
|
|
color_h, color_s, color_v = colors.split(";")
|
2018-07-13 20:01:57 +02:00
|
|
|
except ValueError:
|
2019-07-31 21:25:30 +02:00
|
|
|
color_s, color_v = colors.split(";")
|
2018-07-13 20:01:57 +02:00
|
|
|
color_h = 0
|
2018-03-15 16:45:27 +01:00
|
|
|
|
|
|
|
self._color_h = int(color_h)
|
|
|
|
self._color_s = int(color_s)
|
|
|
|
self._brightness = int(color_v) * 255 / 100
|
|
|
|
|
2017-04-18 18:03:56 +02:00
|
|
|
self._available = True
|
|
|
|
except MyStromConnectionError:
|
2020-04-24 01:00:17 +02:00
|
|
|
_LOGGER.warning("No route to myStrom bulb")
|
2017-04-18 18:03:56 +02:00
|
|
|
self._available = False
|