Improve Bang olufsen media_player dispatcher formatting (#123065)

* Avoid repeating almost the same command 8 times

* Remove debugging
This commit is contained in:
Markus Jacobsen 2024-08-02 17:07:23 +02:00 committed by GitHub
parent a18166e3f8
commit b0ece4bbaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
import json import json
import logging import logging
from typing import Any, cast from typing import Any, cast
@ -137,65 +138,25 @@ class BangOlufsenMediaPlayer(BangOlufsenEntity, MediaPlayerEntity):
"""Turn on the dispatchers.""" """Turn on the dispatchers."""
await self._initialize() await self._initialize()
self.async_on_remove( signal_handlers: dict[str, Callable] = {
async_dispatcher_connect( CONNECTION_STATUS: self._async_update_connection_state,
self.hass, WebsocketNotification.PLAYBACK_ERROR: self._async_update_playback_error,
f"{self._unique_id}_{CONNECTION_STATUS}", WebsocketNotification.PLAYBACK_METADATA: self._async_update_playback_metadata,
self._async_update_connection_state, WebsocketNotification.PLAYBACK_PROGRESS: self._async_update_playback_progress,
) WebsocketNotification.PLAYBACK_STATE: self._async_update_playback_state,
) WebsocketNotification.REMOTE_MENU_CHANGED: self._async_update_sources,
WebsocketNotification.SOURCE_CHANGE: self._async_update_source_change,
WebsocketNotification.VOLUME: self._async_update_volume,
}
self.async_on_remove( for signal, signal_handler in signal_handlers.items():
async_dispatcher_connect( self.async_on_remove(
self.hass, async_dispatcher_connect(
f"{self._unique_id}_{WebsocketNotification.PLAYBACK_ERROR}", self.hass,
self._async_update_playback_error, f"{self._unique_id}_{signal}",
signal_handler,
)
) )
)
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"{self._unique_id}_{WebsocketNotification.PLAYBACK_METADATA}",
self._async_update_playback_metadata,
)
)
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"{self._unique_id}_{WebsocketNotification.PLAYBACK_PROGRESS}",
self._async_update_playback_progress,
)
)
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"{self._unique_id}_{WebsocketNotification.PLAYBACK_STATE}",
self._async_update_playback_state,
)
)
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"{self._unique_id}_{WebsocketNotification.REMOTE_MENU_CHANGED}",
self._async_update_sources,
)
)
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"{self._unique_id}_{WebsocketNotification.SOURCE_CHANGE}",
self._async_update_source_change,
)
)
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"{self._unique_id}_{WebsocketNotification.VOLUME}",
self._async_update_volume,
)
)
async def _initialize(self) -> None: async def _initialize(self) -> None:
"""Initialize connection dependent variables.""" """Initialize connection dependent variables."""