Skip to content

Commit

Permalink
Remove power states as media_player states
Browse files Browse the repository at this point in the history
Remove attempt at static entity_ids for each device
  • Loading branch information
mj23000 committed Dec 21, 2022
1 parent 608560a commit 5e3000f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 47 deletions.
13 changes: 2 additions & 11 deletions custom_components/bangolufsen/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
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
import voluptuous as vol

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -190,19 +188,13 @@ 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
data = user_input
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,
Expand Down Expand Up @@ -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]

Expand All @@ -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],
Expand Down
4 changes: 1 addition & 3 deletions custom_components/bangolufsen/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
10 changes: 0 additions & 10 deletions custom_components/bangolufsen/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
PlaybackContentMetadata,
PlaybackError,
PlaybackProgress,
PowerStateEnum,
RenderingState,
SoundSettings,
Source,
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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(
Expand Down
30 changes: 7 additions & 23 deletions custom_components/bangolufsen/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 5e3000f

Please sign in to comment.