Add light platform to Big Ass Fans (#72382)

Co-authored-by: Erik Montnemery <erik@montnemery.com>
pull/72435/head
J. Nick Koston 2022-05-24 09:20:13 -05:00 committed by GitHub
parent d40cd20692
commit a5402d725f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 104 additions and 0 deletions

View File

@ -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

View File

@ -17,6 +17,7 @@ from .models import BAFData
PLATFORMS: list[Platform] = [
Platform.CLIMATE,
Platform.FAN,
Platform.LIGHT,
Platform.SENSOR,
Platform.SWITCH,
]

View File

@ -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)