Skip to content

Commit

Permalink
Fix beolink relative volume service
Browse files Browse the repository at this point in the history
Update to HA 2023.6
Add gitignore
  • Loading branch information
mj23000 committed Aug 11, 2023
1 parent 53e6318 commit 4878ad0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.DS_Store
*.pyc
21 changes: 10 additions & 11 deletions custom_components/bangolufsen/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from enum import Enum
from enum import Enum, StrEnum
from typing import Final, cast

from mozart_api.models import (
Expand All @@ -29,7 +29,6 @@
)
from mozart_api.mozart_client import MozartClient

from homeassistant.backports.enum import StrEnum
from homeassistant.components.media_player import MediaPlayerState, MediaType
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_NAME
Expand All @@ -51,15 +50,15 @@ class ArtSizeEnum(Enum):
class SourceEnum(StrEnum):
"""Enum used for associating device source ids with friendly names. May not include all sources."""

uriStreamer = "Audio Streamer"
uriStreamer = "Audio Streamer" # noqa: N815
bluetooth = "Bluetooth"
airPlay = "AirPlay"
chromeCast = "Chromecast built-in"
airPlay = "AirPlay" # noqa: N815
chromeCast = "Chromecast built-in" # noqa: N815
spotify = "Spotify Connect"
generator = "Tone Generator"
lineIn = "Line-In"
lineIn = "Line-In" # noqa: N815
spdif = "Optical"
netRadio = "B&O Radio"
netRadio = "B&O Radio" # noqa: N815
local = "Local"
dlna = "DLNA"
qplay = "QPlay"
Expand All @@ -68,7 +67,7 @@ class SourceEnum(StrEnum):
tv = "TV"
deezer = "Deezer"
beolink = "Networklink"
tidalConnect = "Tidal Connect"
tidalConnect = "Tidal Connect" # noqa: N815


class RepeatEnum(StrEnum):
Expand All @@ -94,7 +93,7 @@ class StateEnum(StrEnum):
unknown = MediaPlayerState.IDLE

# Power states
networkStandby = MediaPlayerState.IDLE
networkStandby = MediaPlayerState.IDLE # noqa: N815


# Media types for play_media
Expand All @@ -111,8 +110,8 @@ class BangOlufsenMediaType(StrEnum):
class ProximityEnum(Enum):
"""Proximity detection mapping.."""

proximityPresenceDetected = True
proximityPresenceNotDetected = False
proximityPresenceDetected = True # noqa: N815
proximityPresenceNotDetected = False # noqa: N815


class ModelEnum(StrEnum):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/bangolufsen/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"iot_class": "local_push",
"issue_tracker": "https://github.com/bang-olufsen/bangolufsen-hacs/issues",
"requirements": ["mozart-api==2.5.3.123.1"],
"version": "1.1.0",
"version": "1.1.1",
"zeroconf": ["_bangolufsen._tcp.local."]
}
79 changes: 42 additions & 37 deletions custom_components/bangolufsen/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
ACCEPTED_COMMANDS_LISTS,
BEOLINK_LEADER_COMMAND,
BEOLINK_LISTENER_COMMAND,
BEOLINK_RELATIVE_VOLUME,
BEOLINK_VOLUME,
CONF_BEOLINK_JID,
CONF_DEFAULT_VOLUME,
Expand All @@ -79,7 +80,6 @@
HIDDEN_SOURCE_IDS,
NO_METADATA,
VALID_MEDIA_TYPES,
BEOLINK_RELATIVE_VOLUME,
ArtSizeEnum,
BangOlufsenEntity,
BangOlufsenMediaType,
Expand Down Expand Up @@ -809,7 +809,7 @@ def device_info(self) -> DeviceInfo:
identifiers={(DOMAIN, self._unique_id)},
manufacturer="Bang & Olufsen",
model=self._model,
name=self.name,
name=cast(str, self.name),
sw_version=self._software_status.software_version,
)

Expand Down Expand Up @@ -1052,34 +1052,33 @@ async def async_play_media(
user_flow=UserFlow(user_id=deezer_id), async_req=True
)

else:
# Play a Deezer playlist or album.
if any(match in media_id for match in ("playlist", "album")):
start_from = 0
if "start_from" in kwargs[ATTR_MEDIA_EXTRA]:
start_from = kwargs[ATTR_MEDIA_EXTRA]["start_from"]

self._client.add_to_queue(
play_queue_item=PlayQueueItem(
provider=PlayQueueItemType(value="deezer"),
start_now_from_position=start_from,
type="playlist",
uri=media_id,
),
async_req=True,
)
# Play a Deezer playlist or album.
elif any(match in media_id for match in ("playlist", "album")):
start_from = 0
if "start_from" in kwargs[ATTR_MEDIA_EXTRA]:
start_from = kwargs[ATTR_MEDIA_EXTRA]["start_from"]

self._client.add_to_queue(
play_queue_item=PlayQueueItem(
provider=PlayQueueItemType(value="deezer"),
start_now_from_position=start_from,
type="playlist",
uri=media_id,
),
async_req=True,
)

# Play a Deezer track.
else:
self._client.add_to_queue(
play_queue_item=PlayQueueItem(
provider=PlayQueueItemType(value="deezer"),
start_now_from_position=0,
type="track",
uri=media_id,
),
async_req=True,
)
# Play a Deezer track.
else:
self._client.add_to_queue(
play_queue_item=PlayQueueItem(
provider=PlayQueueItemType(value="deezer"),
start_now_from_position=0,
type="track",
uri=media_id,
),
async_req=True,
)

except ApiException as error:
_LOGGER.error(json.loads(error.body)["message"])
Expand Down Expand Up @@ -1193,14 +1192,11 @@ async def async_beolink_leader_command(
)

# Run the command if leader.
else:
if parameter is not None:
await getattr(self, f"async_{command}")(
parameter_type(parameter)
)
elif parameter is not None:
await getattr(self, f"async_{command}")(parameter_type(parameter))

elif parameter_type is None:
await getattr(self, f"async_{command}")()
elif parameter_type is None:
await getattr(self, f"async_{command}")()

async def async_beolink_set_volume(self, volume_level: str) -> None:
"""Set volume level for all connected Beolink devices."""
Expand All @@ -1226,7 +1222,16 @@ async def async_beolink_set_volume(self, volume_level: str) -> None:

async def async_set_relative_volume_level(self, volume: float) -> None:
"""Set a volume level relative to the current level."""
await self.async_set_volume_level(volume=self.volume_level + volume)

# Ensure that volume level behaves as expected
if self.volume_level + volume >= 1.0:
new_volume = 1.0
elif self.volume_level + volume <= 0:
new_volume = 0
else:
new_volume = self.volume_level + volume

await self.async_set_volume_level(volume=new_volume)

async def async_beolink_set_relative_volume(self, volume_level: str) -> None:
"""Set a volume level to adjust current volume level for all connected Beolink devices."""
Expand Down

0 comments on commit 4878ad0

Please sign in to comment.