From 5e3000f31619cf478914be9d73d9a8378d280953 Mon Sep 17 00:00:00 2001 From: mj23000 Date: Wed, 21 Dec 2022 14:43:50 +0100 Subject: [PATCH] Remove power states as media_player states Remove attempt at static entity_ids for each device --- custom_components/bangolufsen/config_flow.py | 13 ++------ custom_components/bangolufsen/const.py | 4 +-- custom_components/bangolufsen/controller.py | 10 ------- custom_components/bangolufsen/media_player.py | 30 +++++-------------- 4 files changed, 10 insertions(+), 47 deletions(-) diff --git a/custom_components/bangolufsen/config_flow.py b/custom_components/bangolufsen/config_flow.py index 8926d16..3faf0eb 100644 --- a/custom_components/bangolufsen/config_flow.py +++ b/custom_components/bangolufsen/config_flow.py @@ -5,7 +5,6 @@ import logging from typing import Any, TypedDict -from inflection import underscore from mozart_api.exceptions import ApiException from mozart_api.mozart_client import MozartClient from urllib3.exceptions import MaxRetryError, NewConnectionError @@ -13,7 +12,7 @@ from homeassistant.components.zeroconf import ZeroconfServiceInfo from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow -from homeassistant.const import CONF_FRIENDLY_NAME, CONF_HOST, CONF_MODEL, CONF_NAME +from homeassistant.const import CONF_HOST, CONF_MODEL, CONF_NAME from homeassistant.core import callback from homeassistant.data_entry_flow import AbortFlow, FlowResult from homeassistant.helpers import config_validation as cv, selector @@ -115,7 +114,6 @@ class UserInput(TypedDict): """TypedDict for user_input.""" name: str - friendly_name: str volume_step: int default_volume: int max_volume: int @@ -190,7 +188,6 @@ async def async_step_confirm( ) -> FlowResult: """Confirm the configuration of the device.""" if user_input is not None: - # Get the desired friendly name before changing it for generating entity_id self._name = user_input[CONF_NAME] # Make sure that all information is included @@ -198,11 +195,6 @@ async def async_step_confirm( data[CONF_HOST] = self._host data[CONF_MODEL] = self._model data[CONF_BEOLINK_JID] = self._beolink_jid - data[CONF_FRIENDLY_NAME] = self._name - - # Manually define the entity_id - model_name = underscore(self._model.replace(" ", "_")) - data[CONF_NAME] = f"{model_name}_{self._serial_number}" return self.async_create_entry( title=self._name, @@ -253,7 +245,6 @@ async def async_step_init(self, user_input: UserInput | None = None) -> FlowResu data[CONF_MODEL] = self._config_entry.data[CONF_MODEL] data[CONF_BEOLINK_JID] = self._config_entry.data[CONF_BEOLINK_JID] - data[CONF_FRIENDLY_NAME] = user_input[CONF_NAME] if not self.show_advanced_options: data[CONF_HOST] = self._config_entry.data[CONF_HOST] @@ -263,7 +254,7 @@ async def async_step_init(self, user_input: UserInput | None = None) -> FlowResu # Create data schema with the last configuration as default values. data_schema = _config_schema( - name=self._config_entry.data[CONF_FRIENDLY_NAME], + name=self._config_entry.data[CONF_NAME], volume_step=self._config_entry.data[CONF_VOLUME_STEP], default_volume=self._config_entry.data[CONF_DEFAULT_VOLUME], max_volume=self._config_entry.data[CONF_MAX_VOLUME], diff --git a/custom_components/bangolufsen/const.py b/custom_components/bangolufsen/const.py index ec483b3..7c88dc3 100644 --- a/custom_components/bangolufsen/const.py +++ b/custom_components/bangolufsen/const.py @@ -357,9 +357,7 @@ def __init__(self, entry: ConfigEntry) -> None: # Set the configuration variables. self._host: str = self.entry.data[CONF_HOST] self._name: str = self.entry.data[CONF_NAME] - - if isinstance(self.entry.unique_id, str): - self._unique_id: str = self.entry.unique_id + self._unique_id: str = cast(str, self.entry.unique_id) self._dispatchers: list = [] diff --git a/custom_components/bangolufsen/controller.py b/custom_components/bangolufsen/controller.py index 9f30af0..59a5a83 100644 --- a/custom_components/bangolufsen/controller.py +++ b/custom_components/bangolufsen/controller.py @@ -12,7 +12,6 @@ PlaybackContentMetadata, PlaybackError, PlaybackProgress, - PowerStateEnum, RenderingState, SoundSettings, Source, @@ -81,7 +80,6 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None: self._client.get_playback_state_notifications( self.on_playback_state_notification ) - self._client.get_power_state_notifications(self.on_power_state_notification) self._client.get_sound_settings_notifications( self.on_sound_settings_notification ) @@ -276,14 +274,6 @@ def on_playback_state_notification(self, notification: RenderingState) -> None: notification, ) - def on_power_state_notification(self, notification: PowerStateEnum) -> None: - """Send power_state dispatch.""" - async_dispatcher_send( - self.hass, - f"{self._unique_id}_{WebSocketNotification.POWER_STATE}", - notification, - ) - def on_sound_settings_notification(self, notification: SoundSettings) -> None: """Send sound_settings dispatch.""" async_dispatcher_send( diff --git a/custom_components/bangolufsen/media_player.py b/custom_components/bangolufsen/media_player.py index 380d8ab..c7be5d0 100644 --- a/custom_components/bangolufsen/media_player.py +++ b/custom_components/bangolufsen/media_player.py @@ -56,6 +56,7 @@ entity_platform, entity_registry as er, ) +from homeassistant.helpers.device_registry import DeviceEntry from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, async_dispatcher_send, @@ -297,11 +298,6 @@ async def async_added_to_hass(self) -> None: f"{self._unique_id}_{WebSocketNotification.PLAYBACK_STATE}", self._update_playback_state, ), - async_dispatcher_connect( - self.hass, - f"{self._unique_id}_{WebSocketNotification.POWER_STATE}", - self._update_power_state, - ), async_dispatcher_connect( self.hass, f"{self._unique_id}_{WebSocketNotification.SOURCE_CHANGE}", @@ -357,12 +353,13 @@ async def bangolufsen_init(self) -> bool: # Update the device sw version device_registry = dr.async_get(self.hass) - device = device_registry.async_get_device( - identifiers={(DOMAIN, self._unique_id)} + device = cast( + DeviceEntry, + device_registry.async_get_device(identifiers={(DOMAIN, self._unique_id)}), ) # Update the HA device if the sw version does not match - if device and self._software_status.software_version != device.sw_version: + if self._software_status.software_version != device.sw_version: device_registry.async_update_device( device_id=device.id, @@ -647,25 +644,11 @@ async def _update_playback_state(self, data: RenderingState) -> None: """Update _playback_state and related.""" self._playback_state = data - # Update entity state based on the playback and power state. - # The idle state has higher priority than any other playback state - if self._power_state.value == "networkStandby": - return - + # Update entity state based on the playback state. self._state = self._playback_state.value self.async_write_ha_state() - async def _update_power_state(self, data: RenderingState) -> None: - """Update _power_state and related.""" - self._power_state = data - - # Update entity state based on the power state. - if self._power_state.value == "networkStandby": - self._state = cast(MediaPlayerState, StateEnum[self._power_state.value]) - - self.async_write_ha_state() - async def _update_source_change(self, data: Source) -> None: """Update _source_change and related.""" self._source_change = data @@ -883,6 +866,7 @@ def extra_state_attributes(self) -> dict[str, Any] | None: async def async_turn_off(self) -> None: """Set the device to "networkStandby".""" self._client.post_standby(async_req=True) + self._state = cast(MediaPlayerState, StateEnum.networkStandby) async def async_volume_up(self) -> None: """Volume up the on media player."""