hass_flickerstrip/custom_components/flickerstrip/light.py

66 lines
1.7 KiB
Python
Raw Normal View History

2023-01-29 20:57:59 -05:00
from typing import Any
from homeassistant.core import HomeAssistant
from homeassistant.components.light import (
LightEntity,
ATTR_BRIGHTNESS,
ColorMode
)
from .common import FlickerstripEntity
from .const import (
DEFAULT_PORT,
)
class FlickerstripLight(FlickerstripEntity, LightEntity):
"""Flickerstrip class."""
def __init__(
self,
hass: HomeAssistant,
host: str,
port: int = DEFAULT_PORT,
):
super().__init__(host, port)
self.hass = hass
@property
def color_mode():
return ColorMode.RGB
@property
def supported_color_mode():
return [ColorMode.RGB]
@property
def name(self) -> str:
"""Return the display name of this light."""
return self.strip.status.name if len(self.strip.status.name) > 0 else "Flickerstrip"
@property
def brightness(self):
"""Return the brightness of the light."""
return self.strip.status.brightness
@property
def is_on(self) -> bool | None:
"""Return true if light is on."""
return self.strip.status.power
async def async_turn_on(self, **kwargs: Any) -> None:
"""Instruct the light to turn on."""
brightness = kwargs.get(ATTR_BRIGHTNESS, 100)
await self.strip.set_brightness(brightness)
await self.strip.power_on()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Instruct the light to turn off."""
await self.strip.power_off()
async def async_update(self) -> None:
"""Fetch new state data for this light.
This is the only method that should fetch new data for Home Assistant.
"""
await self.strip.force_update()