From 5550b5445b826b2bb093233ab8a84d0e0a905194 Mon Sep 17 00:00:00 2001 From: Milan Meulemans Date: Tue, 23 Nov 2021 01:26:37 +0100 Subject: [PATCH] Add Button platform to Nanoleaf (#60169) Co-authored-by: J. Nick Koston Co-authored-by: Franck Nijhof --- .coveragerc | 2 ++ homeassistant/components/nanoleaf/__init__.py | 7 ++-- homeassistant/components/nanoleaf/button.py | 36 +++++++++++++++++++ homeassistant/components/nanoleaf/entity.py | 22 ++++++++++++ homeassistant/components/nanoleaf/light.py | 25 +++++-------- 5 files changed, 72 insertions(+), 20 deletions(-) create mode 100644 homeassistant/components/nanoleaf/button.py create mode 100644 homeassistant/components/nanoleaf/entity.py diff --git a/.coveragerc b/.coveragerc index bb7e429fa12..fbb761eef1f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -698,6 +698,8 @@ omit = homeassistant/components/myq/light.py homeassistant/components/nad/media_player.py homeassistant/components/nanoleaf/__init__.py + homeassistant/components/nanoleaf/button.py + homeassistant/components/nanoleaf/entity.py homeassistant/components/nanoleaf/light.py homeassistant/components/neato/__init__.py homeassistant/components/neato/api.py diff --git a/homeassistant/components/nanoleaf/__init__.py b/homeassistant/components/nanoleaf/__init__.py index c706f52035f..79bb2577bb7 100644 --- a/homeassistant/components/nanoleaf/__init__.py +++ b/homeassistant/components/nanoleaf/__init__.py @@ -9,6 +9,8 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import DOMAIN +PLATFORMS = ["button", "light"] + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Nanoleaf from a config entry.""" @@ -24,7 +26,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data.setdefault(DOMAIN, {})[entry.entry_id] = nanoleaf - hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, "light") - ) + hass.config_entries.async_setup_platforms(entry, PLATFORMS) + return True diff --git a/homeassistant/components/nanoleaf/button.py b/homeassistant/components/nanoleaf/button.py new file mode 100644 index 00000000000..d85e61fc09c --- /dev/null +++ b/homeassistant/components/nanoleaf/button.py @@ -0,0 +1,36 @@ +"""Support for Nanoleaf buttons.""" + +from aionanoleaf import Nanoleaf + +from homeassistant.components.button import ButtonEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import ENTITY_CATEGORY_CONFIG +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DOMAIN +from .entity import NanoleafEntity + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Set up the Nanoleaf button.""" + nanoleaf: Nanoleaf = hass.data[DOMAIN][entry.entry_id] + async_add_entities([NanoleafIdentifyButton(nanoleaf)]) + + +class NanoleafIdentifyButton(NanoleafEntity, ButtonEntity): + """Representation of a Nanoleaf identify button.""" + + def __init__(self, nanoleaf: Nanoleaf) -> None: + """Initialize the Nanoleaf button.""" + super().__init__(nanoleaf) + self._attr_unique_id = f"{nanoleaf.serial_no}_identify" + self._attr_name = f"Identify {nanoleaf.name}" + self._attr_icon = "mdi:magnify" + self._attr_entity_category = ENTITY_CATEGORY_CONFIG + + async def async_press(self) -> None: + """Identify the Nanoleaf.""" + await self._nanoleaf.identify() diff --git a/homeassistant/components/nanoleaf/entity.py b/homeassistant/components/nanoleaf/entity.py new file mode 100644 index 00000000000..d181aeb12d8 --- /dev/null +++ b/homeassistant/components/nanoleaf/entity.py @@ -0,0 +1,22 @@ +"""Base class for Nanoleaf entity.""" + +from aionanoleaf import Nanoleaf + +from homeassistant.helpers.entity import DeviceInfo, Entity + +from .const import DOMAIN + + +class NanoleafEntity(Entity): + """Representation of a Nanoleaf entity.""" + + def __init__(self, nanoleaf: Nanoleaf) -> None: + """Initialize an Nanoleaf entity.""" + self._nanoleaf = nanoleaf + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, nanoleaf.serial_no)}, + manufacturer=nanoleaf.manufacturer, + model=nanoleaf.model, + name=nanoleaf.name, + sw_version=nanoleaf.firmware_version, + ) diff --git a/homeassistant/components/nanoleaf/light.py b/homeassistant/components/nanoleaf/light.py index 6203431d609..e6a602b51f3 100644 --- a/homeassistant/components/nanoleaf/light.py +++ b/homeassistant/components/nanoleaf/light.py @@ -26,7 +26,6 @@ from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TOKEN from homeassistant.core import HomeAssistant import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.util.color import ( @@ -35,6 +34,7 @@ from homeassistant.util.color import ( ) from .const import DOMAIN +from .entity import NanoleafEntity RESERVED_EFFECTS = ("*Solid*", "*Static*", "*Dynamic*") DEFAULT_NAME = "Nanoleaf" @@ -74,25 +74,16 @@ async def async_setup_entry( async_add_entities([NanoleafLight(nanoleaf)]) -class NanoleafLight(LightEntity): +class NanoleafLight(NanoleafEntity, LightEntity): """Representation of a Nanoleaf Light.""" def __init__(self, nanoleaf: Nanoleaf) -> None: - """Initialize an Nanoleaf light.""" - self._nanoleaf = nanoleaf - self._attr_unique_id = self._nanoleaf.serial_no - self._attr_name = self._nanoleaf.name - self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, self._nanoleaf.serial_no)}, - manufacturer=self._nanoleaf.manufacturer, - model=self._nanoleaf.model, - name=self._nanoleaf.name, - sw_version=self._nanoleaf.firmware_version, - ) - self._attr_min_mireds = math.ceil( - 1000000 / self._nanoleaf.color_temperature_max - ) - self._attr_max_mireds = kelvin_to_mired(self._nanoleaf.color_temperature_min) + """Initialize the Nanoleaf light.""" + super().__init__(nanoleaf) + self._attr_unique_id = nanoleaf.serial_no + self._attr_name = nanoleaf.name + self._attr_min_mireds = math.ceil(1000000 / nanoleaf.color_temperature_max) + self._attr_max_mireds = kelvin_to_mired(nanoleaf.color_temperature_min) @property def brightness(self) -> int: