2021-02-09 21:03:49 +00:00
|
|
|
"""Support for Hive light devices."""
|
2022-04-04 16:24:56 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-02-09 21:03:49 +00:00
|
|
|
from datetime import timedelta
|
2022-08-03 19:26:34 +00:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2021-02-09 21:03:49 +00:00
|
|
|
|
2019-02-14 15:01:46 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_HS_COLOR,
|
2022-04-23 19:14:34 +00:00
|
|
|
ColorMode,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-01-03 14:02:21 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-03-18 22:00:29 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
from . import HiveEntity, refresh_system
|
|
|
|
from .const import ATTR_MODE, DOMAIN
|
2021-02-09 21:03:49 +00:00
|
|
|
|
2022-08-03 19:26:34 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from apyhiveapi import Hive
|
|
|
|
|
2021-02-09 21:03:49 +00:00
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=15)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2022-01-03 14:02:21 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2021-03-15 11:27:10 +00:00
|
|
|
"""Set up Hive thermostat based on a config entry."""
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2022-08-03 19:26:34 +00:00
|
|
|
hive: Hive = hass.data[DOMAIN][entry.entry_id]
|
2021-03-15 11:27:10 +00:00
|
|
|
devices = hive.session.deviceList.get("light")
|
2021-02-09 21:03:49 +00:00
|
|
|
entities = []
|
|
|
|
if devices:
|
|
|
|
for dev in devices:
|
|
|
|
entities.append(HiveDeviceLight(hive, dev))
|
|
|
|
async_add_entities(entities, True)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class HiveDeviceLight(HiveEntity, LightEntity):
|
2018-01-15 22:24:12 +00:00
|
|
|
"""Hive Active Light Device."""
|
|
|
|
|
2022-08-03 19:26:34 +00:00
|
|
|
def __init__(self, hive: Hive, hive_device: dict[str, Any]) -> None:
|
2022-05-29 10:08:50 +00:00
|
|
|
"""Initialise hive light."""
|
|
|
|
super().__init__(hive, hive_device)
|
|
|
|
if self.device["hiveType"] == "warmwhitelight":
|
|
|
|
self._attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
2022-07-15 21:03:22 +00:00
|
|
|
self._attr_color_mode = ColorMode.BRIGHTNESS
|
2022-05-29 10:08:50 +00:00
|
|
|
elif self.device["hiveType"] == "tuneablelight":
|
|
|
|
self._attr_supported_color_modes = {ColorMode.COLOR_TEMP}
|
2022-07-15 21:03:22 +00:00
|
|
|
self._attr_color_mode = ColorMode.COLOR_TEMP
|
2022-05-29 10:08:50 +00:00
|
|
|
elif self.device["hiveType"] == "colourtuneablelight":
|
|
|
|
self._attr_supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS}
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2022-07-15 21:03:22 +00:00
|
|
|
self._attr_min_mireds = 153
|
|
|
|
self._attr_max_mireds = 370
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2019-09-27 21:18:34 +00:00
|
|
|
@refresh_system
|
2022-08-03 19:26:34 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2018-01-15 22:24:12 +00:00
|
|
|
"""Instruct the light to turn on."""
|
|
|
|
new_brightness = None
|
|
|
|
new_color_temp = None
|
|
|
|
new_color = None
|
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
2022-08-03 19:26:34 +00:00
|
|
|
tmp_new_brightness = kwargs[ATTR_BRIGHTNESS]
|
2019-07-31 19:25:30 +00:00
|
|
|
percentage_brightness = (tmp_new_brightness / 255) * 100
|
2018-01-15 22:24:12 +00:00
|
|
|
new_brightness = int(round(percentage_brightness / 5.0) * 5.0)
|
|
|
|
if new_brightness == 0:
|
|
|
|
new_brightness = 5
|
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
2022-08-03 19:26:34 +00:00
|
|
|
tmp_new_color_temp = kwargs[ATTR_COLOR_TEMP]
|
2018-01-15 22:24:12 +00:00
|
|
|
new_color_temp = round(1000000 / tmp_new_color_temp)
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
2022-08-03 19:26:34 +00:00
|
|
|
get_new_color = kwargs[ATTR_HS_COLOR]
|
2018-03-18 22:00:29 +00:00
|
|
|
hue = int(get_new_color[0])
|
|
|
|
saturation = int(get_new_color[1])
|
2021-02-09 21:03:49 +00:00
|
|
|
new_color = (hue, saturation, 100)
|
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
await self.hive.light.turnOn(
|
2021-02-09 21:03:49 +00:00
|
|
|
self.device, new_brightness, new_color_temp, new_color
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2019-09-27 21:18:34 +00:00
|
|
|
@refresh_system
|
2022-08-03 19:26:34 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2018-01-15 22:24:12 +00:00
|
|
|
"""Instruct the light to turn off."""
|
2021-03-15 11:27:10 +00:00
|
|
|
await self.hive.light.turnOff(self.device)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2022-08-03 19:26:34 +00:00
|
|
|
async def async_update(self) -> None:
|
2018-01-27 19:58:27 +00:00
|
|
|
"""Update all Node data from Hive."""
|
2021-02-09 21:03:49 +00:00
|
|
|
await self.hive.session.updateData(self.device)
|
2021-03-15 11:27:10 +00:00
|
|
|
self.device = await self.hive.light.getLight(self.device)
|
2021-02-09 21:03:49 +00:00
|
|
|
self.attributes.update(self.device.get("attributes", {}))
|
2022-05-29 10:08:50 +00:00
|
|
|
self._attr_extra_state_attributes = {
|
|
|
|
ATTR_MODE: self.attributes.get(ATTR_MODE),
|
|
|
|
}
|
|
|
|
self._attr_available = self.device["deviceData"].get("online")
|
|
|
|
if self._attr_available:
|
|
|
|
self._attr_is_on = self.device["status"]["state"]
|
|
|
|
self._attr_brightness = self.device["status"]["brightness"]
|
2022-07-15 21:03:22 +00:00
|
|
|
if self.device["hiveType"] == "tuneablelight":
|
|
|
|
self._attr_color_temp = self.device["status"].get("color_temp")
|
2022-05-29 10:08:50 +00:00
|
|
|
if self.device["hiveType"] == "colourtuneablelight":
|
2022-07-15 21:03:22 +00:00
|
|
|
if self.device["status"]["mode"] == "COLOUR":
|
|
|
|
rgb = self.device["status"]["hs_color"]
|
|
|
|
self._attr_hs_color = color_util.color_RGB_to_hs(*rgb)
|
|
|
|
self._attr_color_mode = ColorMode.HS
|
|
|
|
else:
|
|
|
|
self._attr_color_temp = self.device["status"].get("color_temp")
|
|
|
|
self._attr_color_mode = ColorMode.COLOR_TEMP
|