mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Use runtime data instead of hass.data in jvc projector (#124608)
* feat: use runtime data instead of hass.data * fix: extend ConfigEntry
This commit is contained in:
parent
a68cd712c6
commit
547dbf77aa
@ -15,13 +15,14 @@ from homeassistant.const import (
|
||||
from homeassistant.core import Event, HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import JvcProjectorDataUpdateCoordinator
|
||||
|
||||
type JVCConfigEntry = ConfigEntry[JvcProjectorDataUpdateCoordinator]
|
||||
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.REMOTE, Platform.SELECT, Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: JVCConfigEntry) -> bool:
|
||||
"""Set up integration from a config entry."""
|
||||
device = JvcProjector(
|
||||
host=entry.data[CONF_HOST],
|
||||
@ -43,7 +44,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
coordinator = JvcProjectorDataUpdateCoordinator(hass, device)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
async def disconnect(event: Event) -> None:
|
||||
await device.disconnect()
|
||||
@ -57,9 +58,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: JVCConfigEntry) -> bool:
|
||||
"""Unload config entry."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
await hass.data[DOMAIN][entry.entry_id].device.disconnect()
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
await entry.runtime_data.device.disconnect()
|
||||
return unload_ok
|
||||
|
@ -5,22 +5,20 @@ from __future__ import annotations
|
||||
from jvcprojector import const
|
||||
|
||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import JvcProjectorDataUpdateCoordinator
|
||||
from .const import DOMAIN
|
||||
from . import JVCConfigEntry, JvcProjectorDataUpdateCoordinator
|
||||
from .entity import JvcProjectorEntity
|
||||
|
||||
ON_STATUS = (const.ON, const.WARMING)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant, entry: JVCConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the JVC Projector platform from a config entry."""
|
||||
coordinator: JvcProjectorDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities([JvcBinarySensor(coordinator)])
|
||||
|
||||
|
@ -10,12 +10,11 @@ from typing import Any
|
||||
from jvcprojector import const
|
||||
|
||||
from homeassistant.components.remote import RemoteEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from . import JVCConfigEntry
|
||||
from .entity import JvcProjectorEntity
|
||||
|
||||
COMMANDS = {
|
||||
@ -55,10 +54,10 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant, entry: JVCConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the JVC Projector platform from a config entry."""
|
||||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
async_add_entities([JvcProjectorRemote(coordinator)], True)
|
||||
|
||||
|
||||
|
@ -9,12 +9,10 @@ from typing import Final
|
||||
from jvcprojector import JvcProjector, const
|
||||
|
||||
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import JvcProjectorDataUpdateCoordinator
|
||||
from .const import DOMAIN
|
||||
from . import JVCConfigEntry, JvcProjectorDataUpdateCoordinator
|
||||
from .entity import JvcProjectorEntity
|
||||
|
||||
|
||||
@ -41,11 +39,11 @@ SELECTS: Final[list[JvcProjectorSelectDescription]] = [
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: JVCConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the JVC Projector platform from a config entry."""
|
||||
coordinator: JvcProjectorDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
JvcProjectorSelectEntity(coordinator, description) for description in SELECTS
|
||||
|
@ -9,13 +9,11 @@ from homeassistant.components.sensor import (
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
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 JvcProjectorDataUpdateCoordinator
|
||||
from .const import DOMAIN
|
||||
from . import JVCConfigEntry, JvcProjectorDataUpdateCoordinator
|
||||
from .entity import JvcProjectorEntity
|
||||
|
||||
JVC_SENSORS = (
|
||||
@ -36,10 +34,10 @@ JVC_SENSORS = (
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant, entry: JVCConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up the JVC Projector platform from a config entry."""
|
||||
coordinator: JvcProjectorDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
JvcSensor(coordinator, description) for description in JVC_SENSORS
|
||||
|
@ -5,7 +5,6 @@ from unittest.mock import AsyncMock
|
||||
|
||||
from jvcprojector import JvcProjectorAuthError, JvcProjectorConnectError
|
||||
|
||||
from homeassistant.components.jvc_projector import DOMAIN
|
||||
from homeassistant.components.jvc_projector.coordinator import (
|
||||
INTERVAL_FAST,
|
||||
INTERVAL_SLOW,
|
||||
@ -29,7 +28,7 @@ async def test_coordinator_update(
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert mock_device.get_state.call_count == 3
|
||||
coordinator = hass.data[DOMAIN][mock_integration.entry_id]
|
||||
coordinator = mock_integration.runtime_data
|
||||
assert coordinator.update_interval == INTERVAL_SLOW
|
||||
|
||||
|
||||
@ -69,5 +68,5 @@ async def test_coordinator_device_on(
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
coordinator = hass.data[DOMAIN][mock_config_entry.entry_id]
|
||||
coordinator = mock_config_entry.runtime_data
|
||||
assert coordinator.update_interval == INTERVAL_FAST
|
||||
|
@ -38,8 +38,6 @@ async def test_unload_config_entry(
|
||||
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.entry_id not in hass.data[DOMAIN]
|
||||
|
||||
|
||||
async def test_config_entry_connect_error(
|
||||
hass: HomeAssistant,
|
||||
|
Loading…
x
Reference in New Issue
Block a user