mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Add support for Nanoleaf push updates (#60708)
This commit is contained in:
parent
0c4b308e03
commit
caa04c1866
@ -1,17 +1,31 @@
|
|||||||
"""The Nanoleaf integration."""
|
"""The Nanoleaf integration."""
|
||||||
from aionanoleaf import InvalidToken, Nanoleaf, Unavailable
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from aionanoleaf import EffectsEvent, InvalidToken, Nanoleaf, StateEvent, Unavailable
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_HOST, CONF_TOKEN
|
from homeassistant.const import CONF_HOST, CONF_TOKEN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
PLATFORMS = ["button", "light"]
|
PLATFORMS = ["button", "light"]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class NanoleafEntryData:
|
||||||
|
"""Class for sharing data within the Nanoleaf integration."""
|
||||||
|
|
||||||
|
device: Nanoleaf
|
||||||
|
event_listener: asyncio.Task
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up Nanoleaf from a config entry."""
|
"""Set up Nanoleaf from a config entry."""
|
||||||
nanoleaf = Nanoleaf(
|
nanoleaf = Nanoleaf(
|
||||||
@ -24,8 +38,29 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
except InvalidToken as err:
|
except InvalidToken as err:
|
||||||
raise ConfigEntryAuthFailed from err
|
raise ConfigEntryAuthFailed from err
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = nanoleaf
|
async def _callback_update_light_state(event: StateEvent | EffectsEvent) -> None:
|
||||||
|
"""Receive state and effect event."""
|
||||||
|
async_dispatcher_send(hass, f"{DOMAIN}_update_light_{nanoleaf.serial_no}")
|
||||||
|
|
||||||
|
event_listener = asyncio.create_task(
|
||||||
|
nanoleaf.listen_events(
|
||||||
|
state_callback=_callback_update_light_state,
|
||||||
|
effects_callback=_callback_update_light_state,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = NanoleafEntryData(
|
||||||
|
nanoleaf, event_listener
|
||||||
|
)
|
||||||
|
|
||||||
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
"""Unload a config entry."""
|
||||||
|
await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
entry_data: NanoleafEntryData = hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
|
entry_data.event_listener.cancel()
|
||||||
|
return True
|
||||||
|
@ -8,6 +8,7 @@ from homeassistant.const import ENTITY_CATEGORY_CONFIG
|
|||||||
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 NanoleafEntryData
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .entity import NanoleafEntity
|
from .entity import NanoleafEntity
|
||||||
|
|
||||||
@ -16,8 +17,8 @@ async def async_setup_entry(
|
|||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Nanoleaf button."""
|
"""Set up the Nanoleaf button."""
|
||||||
nanoleaf: Nanoleaf = hass.data[DOMAIN][entry.entry_id]
|
entry_data: NanoleafEntryData = hass.data[DOMAIN][entry.entry_id]
|
||||||
async_add_entities([NanoleafIdentifyButton(nanoleaf)])
|
async_add_entities([NanoleafIdentifyButton(entry_data.device)])
|
||||||
|
|
||||||
|
|
||||||
class NanoleafIdentifyButton(NanoleafEntity, ButtonEntity):
|
class NanoleafIdentifyButton(NanoleafEntity, ButtonEntity):
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Support for Nanoleaf Lights."""
|
"""Support for Nanoleaf Lights."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@ -26,6 +27,7 @@ from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|||||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TOKEN
|
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TOKEN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
from homeassistant.util.color import (
|
from homeassistant.util.color import (
|
||||||
@ -33,6 +35,7 @@ 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 NanoleafEntryData
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .entity import NanoleafEntity
|
from .entity import NanoleafEntity
|
||||||
|
|
||||||
@ -49,6 +52,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
SCAN_INTERVAL = timedelta(minutes=5)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -70,8 +75,8 @@ async def async_setup_entry(
|
|||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Nanoleaf light."""
|
"""Set up the Nanoleaf light."""
|
||||||
nanoleaf: Nanoleaf = hass.data[DOMAIN][entry.entry_id]
|
entry_data: NanoleafEntryData = hass.data[DOMAIN][entry.entry_id]
|
||||||
async_add_entities([NanoleafLight(nanoleaf)])
|
async_add_entities([NanoleafLight(entry_data.device)])
|
||||||
|
|
||||||
|
|
||||||
class NanoleafLight(NanoleafEntity, LightEntity):
|
class NanoleafLight(NanoleafEntity, LightEntity):
|
||||||
@ -188,3 +193,21 @@ class NanoleafLight(NanoleafEntity, LightEntity):
|
|||||||
if not self.available:
|
if not self.available:
|
||||||
_LOGGER.info("Fetching %s data recovered", self.name)
|
_LOGGER.info("Fetching %s data recovered", self.name)
|
||||||
self._attr_available = True
|
self._attr_available = True
|
||||||
|
|
||||||
|
async def async_handle_update(self) -> None:
|
||||||
|
"""Handle state update."""
|
||||||
|
self.async_write_ha_state()
|
||||||
|
if not self.available:
|
||||||
|
_LOGGER.info("Connection to %s recovered", self.name)
|
||||||
|
self._attr_available = True
|
||||||
|
|
||||||
|
async def async_added_to_hass(self) -> None:
|
||||||
|
"""Handle entity being added to Home Assistant."""
|
||||||
|
await super().async_added_to_hass()
|
||||||
|
self.async_on_remove(
|
||||||
|
async_dispatcher_connect(
|
||||||
|
self.hass,
|
||||||
|
f"{DOMAIN}_update_light_{self._nanoleaf.serial_no}",
|
||||||
|
self.async_handle_update,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
@ -25,5 +25,5 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"codeowners": ["@milanmeu"],
|
"codeowners": ["@milanmeu"],
|
||||||
"iot_class": "local_polling"
|
"iot_class": "local_push"
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user