From 9c35e456479398adc29583fde837c4674bd68919 Mon Sep 17 00:00:00 2001 From: Evan Fiordeliso Date: Sun, 29 Jan 2023 20:57:59 -0500 Subject: [PATCH] Move entity to light.py --- custom_components/flickerstrip/common.py | 62 +++--------------- custom_components/flickerstrip/light.py | 65 +++++++++++++++++++ custom_components/flickerstrip/manifest.json | 2 +- requirements_dev.txt | 2 + ...irements.test.txt => requirements_test.txt | 0 5 files changed, 76 insertions(+), 55 deletions(-) create mode 100644 custom_components/flickerstrip/light.py create mode 100644 requirements_dev.txt rename requirements.test.txt => requirements_test.txt (100%) diff --git a/custom_components/flickerstrip/common.py b/custom_components/flickerstrip/common.py index b1cf0e1..5fedf91 100644 --- a/custom_components/flickerstrip/common.py +++ b/custom_components/flickerstrip/common.py @@ -1,58 +1,12 @@ -from typing import Any -import flickerstrip_py as flstrp +from flickerstrip_py import Flickerstrip -from homeassistant.core import HomeAssistant -from homeassistant.components.light import ( - ATTR_BRIGHTNESS, - LightEntity, -) - -from .const import ( - DEFAULT_PORT, -) +from homeassistant.helpers.entity import Entity -class Flickerstrip(LightEntity): - """Flickerstrip class.""" +class FlickerstripEntity(Entity): + """Base flickerstrip entity.""" - def __init__( - self, - hass: HomeAssistant, - host: str, - port: int = DEFAULT_PORT, - ): - self.hass = hass - self.host = host - self.port = port - self._flickerstrip = flstrp.Flickerstrip(host) - - @property - def name(self) -> str: - """Return the display name of this light.""" - return self._name - - @property - def brightness(self): - """Return the brightness of the light.""" - return self._flickerstrip.status.brightness - - @property - def is_on(self) -> bool | None: - """Return true if light is on.""" - return self._flickerstrip.status.power - - def turn_on(self, **kwargs: Any) -> None: - """Instruct the light to turn on.""" - brightness = kwargs.get(ATTR_BRIGHTNESS, 100) - self._flickerstrip.set_brightness(brightness) - self._flickerstrip.power_on() - - def turn_off(self, **kwargs: Any) -> None: - """Instruct the light to turn off.""" - self._flickerstrip.power_off() - - def update(self) -> None: - """Fetch new state data for this light. - This is the only method that should fetch new data for Home Assistant. - """ - self._flickerstrip.force_update() + def __init__(self, host: str, port: int = 80): + self.host: str = host + self.port: int = port + self.strip: Flickerstrip = Flickerstrip(host, port) diff --git a/custom_components/flickerstrip/light.py b/custom_components/flickerstrip/light.py new file mode 100644 index 0000000..963ee6e --- /dev/null +++ b/custom_components/flickerstrip/light.py @@ -0,0 +1,65 @@ +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() diff --git a/custom_components/flickerstrip/manifest.json b/custom_components/flickerstrip/manifest.json index 7f2b629..79af2cd 100644 --- a/custom_components/flickerstrip/manifest.json +++ b/custom_components/flickerstrip/manifest.json @@ -14,7 +14,7 @@ } ], "requirements": [ - "git+https://git.fifitido.net/lib/flickerstrip-py@d808ca8ac8#egg=flickerstrip-py" + "flickerstrip-py @ git+https://git.fifitido.net/lib/flickerstrip-py" ], "version": "1.0.0" } diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..17d3553 --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1,2 @@ +homeassistant==2023.1.7 +flickerstrip-py @ git+https://git.fifitido.net/lib/flickerstrip-py \ No newline at end of file diff --git a/requirements.test.txt b/requirements_test.txt similarity index 100% rename from requirements.test.txt rename to requirements_test.txt