mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
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>
This commit is contained in:
parent
3224224bf8
commit
4d7a857555
@ -4,7 +4,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from dataclasses import dataclass
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from aionanoleaf import EffectsEvent, Nanoleaf, StateEvent, TouchEvent
|
from aionanoleaf import EffectsEvent, Nanoleaf, StateEvent, TouchEvent
|
||||||
@ -29,15 +28,10 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
PLATFORMS = [Platform.BUTTON, Platform.LIGHT]
|
PLATFORMS = [Platform.BUTTON, Platform.LIGHT]
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
type NanoleafConfigEntry = ConfigEntry[NanoleafCoordinator]
|
||||||
class NanoleafEntryData:
|
|
||||||
"""Class for sharing data within the Nanoleaf integration."""
|
|
||||||
|
|
||||||
device: Nanoleaf
|
|
||||||
coordinator: 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."""
|
"""Set up Nanoleaf from a config entry."""
|
||||||
nanoleaf = Nanoleaf(
|
nanoleaf = Nanoleaf(
|
||||||
async_get_clientsession(hass), entry.data[CONF_HOST], entry.data[CONF_TOKEN]
|
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)
|
entry.async_on_unload(_cancel_listener)
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = NanoleafEntryData(
|
entry.runtime_data = coordinator
|
||||||
nanoleaf, coordinator
|
|
||||||
)
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
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."""
|
"""Unload a config entry."""
|
||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
return unload_ok
|
|
||||||
|
@ -1,22 +1,22 @@
|
|||||||
"""Support for Nanoleaf buttons."""
|
"""Support for Nanoleaf buttons."""
|
||||||
|
|
||||||
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
|
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import NanoleafCoordinator, NanoleafEntryData
|
from . import NanoleafConfigEntry
|
||||||
from .const import DOMAIN
|
from .coordinator import NanoleafCoordinator
|
||||||
from .entity import NanoleafEntity
|
from .entity import NanoleafEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: NanoleafConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Nanoleaf button."""
|
"""Set up the Nanoleaf button."""
|
||||||
entry_data: NanoleafEntryData = hass.data[DOMAIN][entry.entry_id]
|
async_add_entities([NanoleafIdentifyButton(entry.runtime_data)])
|
||||||
async_add_entities([NanoleafIdentifyButton(entry_data.coordinator)])
|
|
||||||
|
|
||||||
|
|
||||||
class NanoleafIdentifyButton(NanoleafEntity, ButtonEntity):
|
class NanoleafIdentifyButton(NanoleafEntity, ButtonEntity):
|
||||||
|
@ -4,22 +4,19 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from aionanoleaf import Nanoleaf
|
|
||||||
|
|
||||||
from homeassistant.components.diagnostics import async_redact_data
|
from homeassistant.components.diagnostics import async_redact_data
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_TOKEN
|
from homeassistant.const import CONF_TOKEN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import NanoleafConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: NanoleafConfigEntry,
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
device: Nanoleaf = hass.data[DOMAIN][config_entry.entry_id].device
|
device = config_entry.runtime_data.nanoleaf
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"info": async_redact_data(config_entry.as_dict(), (CONF_TOKEN, "title")),
|
"info": async_redact_data(config_entry.as_dict(), (CONF_TOKEN, "title")),
|
||||||
|
@ -15,7 +15,6 @@ from homeassistant.components.light import (
|
|||||||
LightEntity,
|
LightEntity,
|
||||||
LightEntityFeature,
|
LightEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util.color import (
|
from homeassistant.util.color import (
|
||||||
@ -23,8 +22,8 @@ from homeassistant.util.color import (
|
|||||||
color_temperature_mired_to_kelvin as mired_to_kelvin,
|
color_temperature_mired_to_kelvin as mired_to_kelvin,
|
||||||
)
|
)
|
||||||
|
|
||||||
from . import NanoleafCoordinator, NanoleafEntryData
|
from . import NanoleafConfigEntry
|
||||||
from .const import DOMAIN
|
from .coordinator import NanoleafCoordinator
|
||||||
from .entity import NanoleafEntity
|
from .entity import NanoleafEntity
|
||||||
|
|
||||||
RESERVED_EFFECTS = ("*Solid*", "*Static*", "*Dynamic*")
|
RESERVED_EFFECTS = ("*Solid*", "*Static*", "*Dynamic*")
|
||||||
@ -32,11 +31,12 @@ DEFAULT_NAME = "Nanoleaf"
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: NanoleafConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Nanoleaf light."""
|
"""Set up the Nanoleaf light."""
|
||||||
entry_data: NanoleafEntryData = hass.data[DOMAIN][entry.entry_id]
|
async_add_entities([NanoleafLight(entry.runtime_data)])
|
||||||
async_add_entities([NanoleafLight(entry_data.coordinator)])
|
|
||||||
|
|
||||||
|
|
||||||
class NanoleafLight(NanoleafEntity, LightEntity):
|
class NanoleafLight(NanoleafEntity, LightEntity):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user