2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Yeelight Sunflower color bulbs (not Yeelight Blue or WiFi)."""
|
2022-01-04 09:52:30 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-02-18 22:05:55 +00:00
|
|
|
import logging
|
2017-03-26 13:50:40 +00:00
|
|
|
|
2017-02-18 22:05:55 +00:00
|
|
|
import voluptuous as vol
|
2019-12-09 13:57:42 +00:00
|
|
|
import yeelightsunflower
|
2017-02-18 22:05:55 +00:00
|
|
|
|
2017-03-26 13:50:40 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
2019-12-09 13:57:42 +00:00
|
|
|
ATTR_HS_COLOR,
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA,
|
2019-12-09 13:57:42 +00:00
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-03-26 13:50:40 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
2022-01-04 09:52:30 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-09 13:57:42 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-04 09:52:30 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-03-18 22:00:29 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2017-02-18 22:05:55 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_YEELIGHT_SUNFLOWER = SUPPORT_BRIGHTNESS | SUPPORT_COLOR
|
2017-02-25 12:24:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_HOST): cv.string})
|
2017-02-18 22:05:55 +00:00
|
|
|
|
|
|
|
|
2022-01-04 09:52:30 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-02-25 12:24:43 +00:00
|
|
|
"""Set up the Yeelight Sunflower Light platform."""
|
2017-02-18 22:05:55 +00:00
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
hub = yeelightsunflower.Hub(host)
|
|
|
|
|
|
|
|
if not hub.available:
|
2017-03-26 13:50:40 +00:00
|
|
|
_LOGGER.error("Could not connect to Yeelight Sunflower hub")
|
2022-01-04 09:52:30 +00:00
|
|
|
return
|
2017-02-18 22:05:55 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(SunflowerBulb(light) for light in hub.get_lights())
|
2017-02-18 22:05:55 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class SunflowerBulb(LightEntity):
|
2017-02-18 22:05:55 +00:00
|
|
|
"""Representation of a Yeelight Sunflower Light."""
|
|
|
|
|
|
|
|
def __init__(self, light):
|
|
|
|
"""Initialize a Yeelight Sunflower bulb."""
|
|
|
|
self._light = light
|
|
|
|
self._available = light.available
|
|
|
|
self._brightness = light.brightness
|
|
|
|
self._is_on = light.is_on
|
2020-06-05 06:40:50 +00:00
|
|
|
self._rgb_color = light.rgb_color
|
2020-06-01 16:36:43 +00:00
|
|
|
self._unique_id = light.zid
|
2017-02-18 22:05:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the display name of this light."""
|
2019-09-03 19:15:31 +00:00
|
|
|
return f"sunflower_{self._light.zid}"
|
2017-02-18 22:05:55 +00:00
|
|
|
|
2020-06-01 16:36:43 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique ID of this light."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2017-02-18 22:05:55 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self._available
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
|
|
|
return self._is_on
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the brightness is 0-255; Yeelight's brightness is 0-100."""
|
2017-02-18 22:05:55 +00:00
|
|
|
return int(self._brightness / 100 * 255)
|
|
|
|
|
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def hs_color(self):
|
2017-02-18 22:05:55 +00:00
|
|
|
"""Return the color property."""
|
2020-06-05 06:40:50 +00:00
|
|
|
return color_util.color_RGB_to_hs(*self._rgb_color)
|
2017-02-18 22:05:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_YEELIGHT_SUNFLOWER
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Instruct the light to turn on, optionally set colour/brightness."""
|
|
|
|
# when no arguments, just turn light on (full brightness)
|
|
|
|
if not kwargs:
|
|
|
|
self._light.turn_on()
|
|
|
|
else:
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs and ATTR_BRIGHTNESS in kwargs:
|
|
|
|
rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
|
2017-02-18 22:05:55 +00:00
|
|
|
bright = int(kwargs[ATTR_BRIGHTNESS] / 255 * 100)
|
|
|
|
self._light.set_all(rgb[0], rgb[1], rgb[2], bright)
|
2018-03-18 22:00:29 +00:00
|
|
|
elif ATTR_HS_COLOR in kwargs:
|
|
|
|
rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
|
2017-02-18 22:05:55 +00:00
|
|
|
self._light.set_rgb_color(rgb[0], rgb[1], rgb[2])
|
|
|
|
elif ATTR_BRIGHTNESS in kwargs:
|
|
|
|
bright = int(kwargs[ATTR_BRIGHTNESS] / 255 * 100)
|
|
|
|
self._light.set_brightness(bright)
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Instruct the light to turn off."""
|
|
|
|
self._light.turn_off()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Fetch new state data for this light and update local values."""
|
|
|
|
self._light.update()
|
|
|
|
self._available = self._light.available
|
|
|
|
self._brightness = self._light.brightness
|
|
|
|
self._is_on = self._light.is_on
|
2020-06-05 06:40:50 +00:00
|
|
|
self._rgb_color = self._light.rgb_color
|