Use runtime_data for elgato (#116614)

This commit is contained in:
Marc Mueller 2024-05-06 19:12:01 +02:00 committed by GitHub
parent 95405ba6bb
commit 8c053a351c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 22 additions and 30 deletions

View file

@ -4,25 +4,24 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import DOMAIN
from .coordinator import ElgatoDataUpdateCoordinator from .coordinator import ElgatoDataUpdateCoordinator
PLATFORMS = [Platform.BUTTON, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH] PLATFORMS = [Platform.BUTTON, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH]
ElgatorConfigEntry = ConfigEntry[ElgatoDataUpdateCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: ElgatorConfigEntry) -> bool:
"""Set up Elgato Light from a config entry.""" """Set up Elgato Light from a config entry."""
coordinator = ElgatoDataUpdateCoordinator(hass, entry) coordinator = ElgatoDataUpdateCoordinator(hass, entry)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator entry.runtime_data = 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: ElgatorConfigEntry) -> bool:
"""Unload Elgato Light config entry.""" """Unload Elgato Light config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
del hass.data[DOMAIN][entry.entry_id]
return unload_ok

View file

@ -13,13 +13,12 @@ from homeassistant.components.button import (
ButtonEntity, ButtonEntity,
ButtonEntityDescription, ButtonEntityDescription,
) )
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.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN from . import ElgatorConfigEntry
from .coordinator import ElgatoDataUpdateCoordinator from .coordinator import ElgatoDataUpdateCoordinator
from .entity import ElgatoEntity from .entity import ElgatoEntity
@ -49,11 +48,11 @@ BUTTONS = [
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: ElgatorConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up Elgato button based on a config entry.""" """Set up Elgato button based on a config entry."""
coordinator: ElgatoDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
async_add_entities( async_add_entities(
ElgatoButtonEntity( ElgatoButtonEntity(
coordinator=coordinator, coordinator=coordinator,

View file

@ -4,18 +4,16 @@ from __future__ import annotations
from typing import Any from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import DOMAIN from . import ElgatorConfigEntry
from .coordinator import ElgatoDataUpdateCoordinator
async def async_get_config_entry_diagnostics( async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry hass: HomeAssistant, entry: ElgatorConfigEntry
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Return diagnostics for a config entry.""" """Return diagnostics for a config entry."""
coordinator: ElgatoDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
return { return {
"info": coordinator.data.info.to_dict(), "info": coordinator.data.info.to_dict(),
"state": coordinator.data.state.to_dict(), "state": coordinator.data.state.to_dict(),

View file

@ -13,7 +13,6 @@ from homeassistant.components.light import (
ColorMode, ColorMode,
LightEntity, LightEntity,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import ( from homeassistant.helpers.entity_platform import (
@ -21,7 +20,8 @@ from homeassistant.helpers.entity_platform import (
async_get_current_platform, async_get_current_platform,
) )
from .const import DOMAIN, SERVICE_IDENTIFY from . import ElgatorConfigEntry
from .const import SERVICE_IDENTIFY
from .coordinator import ElgatoDataUpdateCoordinator from .coordinator import ElgatoDataUpdateCoordinator
from .entity import ElgatoEntity from .entity import ElgatoEntity
@ -30,11 +30,11 @@ PARALLEL_UPDATES = 1
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: ElgatorConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up Elgato Light based on a config entry.""" """Set up Elgato Light based on a config entry."""
coordinator: ElgatoDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
async_add_entities([ElgatoLight(coordinator)]) async_add_entities([ElgatoLight(coordinator)])
platform = async_get_current_platform() platform = async_get_current_platform()

View file

@ -11,7 +11,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
PERCENTAGE, PERCENTAGE,
EntityCategory, EntityCategory,
@ -22,7 +21,7 @@ from homeassistant.const import (
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 .const import DOMAIN from . import ElgatorConfigEntry
from .coordinator import ElgatoData, ElgatoDataUpdateCoordinator from .coordinator import ElgatoData, ElgatoDataUpdateCoordinator
from .entity import ElgatoEntity from .entity import ElgatoEntity
@ -102,11 +101,11 @@ SENSORS = [
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: ElgatorConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up Elgato sensor based on a config entry.""" """Set up Elgato sensor based on a config entry."""
coordinator: ElgatoDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
async_add_entities( async_add_entities(
ElgatoSensorEntity( ElgatoSensorEntity(

View file

@ -9,13 +9,12 @@ from typing import Any
from elgato import Elgato, ElgatoError from elgato import Elgato, ElgatoError
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
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.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN from . import ElgatorConfigEntry
from .coordinator import ElgatoData, ElgatoDataUpdateCoordinator from .coordinator import ElgatoData, ElgatoDataUpdateCoordinator
from .entity import ElgatoEntity from .entity import ElgatoEntity
@ -53,11 +52,11 @@ SWITCHES = [
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: ElgatorConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up Elgato switches based on a config entry.""" """Set up Elgato switches based on a config entry."""
coordinator: ElgatoDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
async_add_entities( async_add_entities(
ElgatoSwitchEntity( ElgatoSwitchEntity(

View file

@ -4,7 +4,6 @@ from unittest.mock import MagicMock
from elgato import ElgatoConnectionError from elgato import ElgatoConnectionError
from homeassistant.components.elgato.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -27,7 +26,6 @@ async def test_load_unload_config_entry(
await hass.config_entries.async_unload(mock_config_entry.entry_id) await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert not hass.data.get(DOMAIN)
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED assert mock_config_entry.state is ConfigEntryState.NOT_LOADED