Add Button platform to Nanoleaf (#60169)

Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
pull/60181/head
Milan Meulemans 2021-11-23 01:26:37 +01:00 committed by GitHub
parent 9e606abb0c
commit 5550b5445b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 20 deletions

View File

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

View File

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

View File

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

View File

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

View File

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