From a5402d725fe1e92f7043fbe48b577c11e5e0d939 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 24 May 2022 09:20:13 -0500 Subject: [PATCH] Add light platform to Big Ass Fans (#72382) Co-authored-by: Erik Montnemery --- .coveragerc | 1 + homeassistant/components/baf/__init__.py | 1 + homeassistant/components/baf/light.py | 102 +++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 homeassistant/components/baf/light.py diff --git a/.coveragerc b/.coveragerc index dd03c3782c0..ca6c57fb341 100644 --- a/.coveragerc +++ b/.coveragerc @@ -97,6 +97,7 @@ omit = homeassistant/components/baf/climate.py homeassistant/components/baf/entity.py homeassistant/components/baf/fan.py + homeassistant/components/baf/light.py homeassistant/components/baf/sensor.py homeassistant/components/baf/switch.py homeassistant/components/baidu/tts.py diff --git a/homeassistant/components/baf/__init__.py b/homeassistant/components/baf/__init__.py index 30de2582af1..7127228383a 100644 --- a/homeassistant/components/baf/__init__.py +++ b/homeassistant/components/baf/__init__.py @@ -17,6 +17,7 @@ from .models import BAFData PLATFORMS: list[Platform] = [ Platform.CLIMATE, Platform.FAN, + Platform.LIGHT, Platform.SENSOR, Platform.SWITCH, ] diff --git a/homeassistant/components/baf/light.py b/homeassistant/components/baf/light.py new file mode 100644 index 00000000000..b177d383cd5 --- /dev/null +++ b/homeassistant/components/baf/light.py @@ -0,0 +1,102 @@ +"""Support for Big Ass Fans lights.""" +from __future__ import annotations + +from typing import Any + +from aiobafi6 import Device, OffOnAuto + +from homeassistant import config_entries +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, + ATTR_COLOR_TEMP, + ColorMode, + LightEntity, +) +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.util.color import ( + color_temperature_kelvin_to_mired, + color_temperature_mired_to_kelvin, +) + +from .const import DOMAIN +from .entity import BAFEntity +from .models import BAFData + + +async def async_setup_entry( + hass: HomeAssistant, + entry: config_entries.ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up BAF lights.""" + data: BAFData = hass.data[DOMAIN][entry.entry_id] + if data.device.has_light: + klass = BAFFanLight if data.device.has_fan else BAFStandaloneLight + async_add_entities([klass(data.device)]) + + +class BAFLight(BAFEntity, LightEntity): + """Representation of a Big Ass Fans light.""" + + @callback + def _async_update_attrs(self) -> None: + """Update attrs from device.""" + self._attr_is_on = self._device.light_mode == OffOnAuto.ON + if self._device.light_brightness_level is not None: + self._attr_brightness = round( + self._device.light_brightness_level / 16 * 255 + ) + + async def async_turn_on(self, **kwargs: Any) -> None: + """Turn on the light.""" + if (brightness := kwargs.get(ATTR_BRIGHTNESS)) is not None: + self._device.light_brightness_level = max(int(brightness / 255 * 16), 1) + else: + self._device.light_mode = OffOnAuto.ON + + async def async_turn_off(self, **kwargs: Any) -> None: + """Turn off the light.""" + self._device.light_mode = OffOnAuto.OFF + + +class BAFFanLight(BAFLight): + """Representation of a Big Ass Fans light on a fan.""" + + def __init__(self, device: Device) -> None: + """Init a fan light.""" + super().__init__(device, device.name) + self._attr_supported_color_modes = {ColorMode.BRIGHTNESS} + self._attr_color_mode = ColorMode.BRIGHTNESS + + +class BAFStandaloneLight(BAFLight): + """Representation of a Big Ass Fans light.""" + + def __init__(self, device: Device) -> None: + """Init a standalone light.""" + super().__init__(device, f"{device.name} Light") + self._attr_supported_color_modes = {ColorMode.COLOR_TEMP} + self._attr_color_mode = ColorMode.COLOR_TEMP + self._attr_min_mireds = color_temperature_kelvin_to_mired( + device.light_warmest_color_temperature + ) + self._attr_max_mireds = color_temperature_kelvin_to_mired( + device.light_coolest_color_temperature + ) + + @callback + def _async_update_attrs(self) -> None: + """Update attrs from device.""" + super()._async_update_attrs() + self._attr_color_temp = color_temperature_kelvin_to_mired( + self._device.light_color_temperature + ) + + async def async_turn_on(self, **kwargs: Any) -> None: + """Turn on the light.""" + if (color_temp := kwargs.get(ATTR_COLOR_TEMP)) is not None: + self._device.light_color_temperature = color_temperature_mired_to_kelvin( + color_temp + ) + await super().async_turn_on(**kwargs)