Use runtimedata in nanoleaf (#120009)
* Use runtimedata in nanoleaf * Update homeassistant/components/nanoleaf/light.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>pull/120016/head^2
parent
3224224bf8
commit
4d7a857555
|
@ -4,7 +4,6 @@ from __future__ import annotations
|
|||
|
||||
import asyncio
|
||||
from contextlib import suppress
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from aionanoleaf import EffectsEvent, Nanoleaf, StateEvent, TouchEvent
|
||||
|
@ -29,15 +28,10 @@ _LOGGER = logging.getLogger(__name__)
|
|||
PLATFORMS = [Platform.BUTTON, Platform.LIGHT]
|
||||
|
||||
|
||||
@dataclass
|
||||
class NanoleafEntryData:
|
||||
"""Class for sharing data within the Nanoleaf integration."""
|
||||
|
||||
device: Nanoleaf
|
||||
coordinator: NanoleafCoordinator
|
||||
type NanoleafConfigEntry = ConfigEntry[NanoleafCoordinator]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: NanoleafConfigEntry) -> bool:
|
||||
"""Set up Nanoleaf from a config entry."""
|
||||
nanoleaf = Nanoleaf(
|
||||
async_get_clientsession(hass), entry.data[CONF_HOST], entry.data[CONF_TOKEN]
|
||||
|
@ -87,17 +81,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
|
||||
entry.async_on_unload(_cancel_listener)
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = NanoleafEntryData(
|
||||
nanoleaf, coordinator
|
||||
)
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: NanoleafConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
"""Support for Nanoleaf buttons."""
|
||||
|
||||
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import NanoleafCoordinator, NanoleafEntryData
|
||||
from .const import DOMAIN
|
||||
from . import NanoleafConfigEntry
|
||||
from .coordinator import NanoleafCoordinator
|
||||
from .entity import NanoleafEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant,
|
||||
entry: NanoleafConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Nanoleaf button."""
|
||||
entry_data: NanoleafEntryData = hass.data[DOMAIN][entry.entry_id]
|
||||
async_add_entities([NanoleafIdentifyButton(entry_data.coordinator)])
|
||||
async_add_entities([NanoleafIdentifyButton(entry.runtime_data)])
|
||||
|
||||
|
||||
class NanoleafIdentifyButton(NanoleafEntity, ButtonEntity):
|
||||
|
|
|
@ -4,22 +4,19 @@ from __future__ import annotations
|
|||
|
||||
from typing import Any
|
||||
|
||||
from aionanoleaf import Nanoleaf
|
||||
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_TOKEN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from . import NanoleafConfigEntry
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: NanoleafConfigEntry,
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
device: Nanoleaf = hass.data[DOMAIN][config_entry.entry_id].device
|
||||
device = config_entry.runtime_data.nanoleaf
|
||||
|
||||
return {
|
||||
"info": async_redact_data(config_entry.as_dict(), (CONF_TOKEN, "title")),
|
||||
|
|
|
@ -15,7 +15,6 @@ from homeassistant.components.light import (
|
|||
LightEntity,
|
||||
LightEntityFeature,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util.color import (
|
||||
|
@ -23,8 +22,8 @@ from homeassistant.util.color import (
|
|||
color_temperature_mired_to_kelvin as mired_to_kelvin,
|
||||
)
|
||||
|
||||
from . import NanoleafCoordinator, NanoleafEntryData
|
||||
from .const import DOMAIN
|
||||
from . import NanoleafConfigEntry
|
||||
from .coordinator import NanoleafCoordinator
|
||||
from .entity import NanoleafEntity
|
||||
|
||||
RESERVED_EFFECTS = ("*Solid*", "*Static*", "*Dynamic*")
|
||||
|
@ -32,11 +31,12 @@ DEFAULT_NAME = "Nanoleaf"
|
|||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant,
|
||||
entry: NanoleafConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Nanoleaf light."""
|
||||
entry_data: NanoleafEntryData = hass.data[DOMAIN][entry.entry_id]
|
||||
async_add_entities([NanoleafLight(entry_data.coordinator)])
|
||||
async_add_entities([NanoleafLight(entry.runtime_data)])
|
||||
|
||||
|
||||
class NanoleafLight(NanoleafEntity, LightEntity):
|
||||
|
|
Loading…
Reference in New Issue