mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Use runtime_data in bang_olufsen (#129037)
This commit is contained in:
parent
756a866ffd
commit
5a0e47be48
@ -31,10 +31,12 @@ class BangOlufsenData:
|
||||
client: MozartClient
|
||||
|
||||
|
||||
type BangOlufsenConfigEntry = ConfigEntry[BangOlufsenData]
|
||||
|
||||
PLATFORMS = [Platform.MEDIA_PLAYER]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: BangOlufsenConfigEntry) -> bool:
|
||||
"""Set up from a config entry."""
|
||||
|
||||
# Remove casts to str
|
||||
@ -67,10 +69,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
websocket = BangOlufsenWebsocket(hass, entry, client)
|
||||
|
||||
# Add the websocket and API client
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = BangOlufsenData(
|
||||
websocket,
|
||||
client,
|
||||
)
|
||||
entry.runtime_data = BangOlufsenData(websocket, client)
|
||||
|
||||
# Start WebSocket connection
|
||||
await client.connect_notifications(remote_control=True, reconnect=True)
|
||||
@ -80,15 +79,12 @@ 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: BangOlufsenConfigEntry
|
||||
) -> bool:
|
||||
"""Unload a config entry."""
|
||||
# Close the API client and WebSocket notification listener
|
||||
hass.data[DOMAIN][entry.entry_id].client.disconnect_notifications()
|
||||
await hass.data[DOMAIN][entry.entry_id].client.close_api_client()
|
||||
entry.runtime_data.client.disconnect_notifications()
|
||||
await entry.runtime_data.client.close_api_client()
|
||||
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
@ -56,7 +56,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from . import BangOlufsenData
|
||||
from . import BangOlufsenConfigEntry
|
||||
from .const import (
|
||||
BANG_OLUFSEN_STATES,
|
||||
CONF_BEOLINK_JID,
|
||||
@ -96,14 +96,16 @@ BANG_OLUFSEN_FEATURES = (
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: BangOlufsenConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up a Media Player entity from config entry."""
|
||||
data: BangOlufsenData = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
||||
# Add MediaPlayer entity
|
||||
async_add_entities(new_entities=[BangOlufsenMediaPlayer(config_entry, data.client)])
|
||||
async_add_entities(
|
||||
new_entities=[
|
||||
BangOlufsenMediaPlayer(config_entry, config_entry.runtime_data.client)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class BangOlufsenMediaPlayer(BangOlufsenEntity, MediaPlayerEntity):
|
||||
|
@ -85,6 +85,7 @@ async def test_unload_entry(
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
|
||||
assert mock_config_entry.state == ConfigEntryState.LOADED
|
||||
assert hasattr(mock_config_entry, "runtime_data")
|
||||
|
||||
# Unload entry
|
||||
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||
@ -94,5 +95,5 @@ async def test_unload_entry(
|
||||
assert mock_mozart_client.close_api_client.call_count == 1
|
||||
|
||||
# Ensure that the entry is not loaded and has been removed from hass
|
||||
assert mock_config_entry.entry_id not in hass.data[DOMAIN]
|
||||
assert not hasattr(mock_config_entry, "runtime_data")
|
||||
assert mock_config_entry.state == ConfigEntryState.NOT_LOADED
|
||||
|
Loading…
x
Reference in New Issue
Block a user