Skip to content

Commit

Permalink
Add beolink_set_relative_volume custom service
Browse files Browse the repository at this point in the history
Formatting tweaks
  • Loading branch information
mj23000 committed May 30, 2023
1 parent 1e4179a commit 53e6318
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 20 deletions.
7 changes: 1 addition & 6 deletions custom_components/bangolufsen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
from urllib3.exceptions import MaxRetryError

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_HOST,
CONF_MODEL,
CONF_NAME,
Platform,
)
from homeassistant.const import CONF_HOST, CONF_MODEL, CONF_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.event import async_call_later

Expand Down
2 changes: 1 addition & 1 deletion custom_components/bangolufsen/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
MAX_RETRY_ERROR,
MAX_VOLUME_RANGE,
NEW_CONNECTION_ERROR,
NOT_MOZART_DEVICE,
VALUE_ERROR,
VOLUME_STEP_RANGE,
NOT_MOZART_DEVICE,
)


Expand Down
8 changes: 7 additions & 1 deletion custom_components/bangolufsen/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,19 @@ class SupportEnum(Enum):
BEOLINK_LEADER_COMMAND: Final[str] = "BEOLINK_LEADER_COMMAND"
BEOLINK_LISTENER_COMMAND: Final[str] = "BEOLINK_LISTENER_COMMAND"
BEOLINK_VOLUME: Final[str] = "BEOLINK_VOLUME"
BEOLINK_RELATIVE_VOLUME: Final[str] = "BEOLINK_RELATIVE_VOLUME"


# Misc.
NO_METADATA: Final[tuple] = (None, "", 0)

# Valid commands and their expected parameter type for beolink_command service
FLOAT_PARAMETERS: Final[tuple] = ("set_volume_level", "media_seek", float)
FLOAT_PARAMETERS: Final[tuple] = (
"set_volume_level",
"media_seek",
"set_relative_volume_level",
float,
)
BOOL_PARAMETERS: Final[tuple] = ("mute_volume", bool)
STR_PARAMETERS: Final[tuple] = ("select_source", str)
NONE_PARAMETERS: Final[tuple] = (
Expand Down
2 changes: 1 addition & 1 deletion custom_components/bangolufsen/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from __future__ import annotations

from datetime import timedelta, datetime
from datetime import datetime, timedelta
import logging
from typing import TypedDict

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.0.6",
"version": "1.1.0",
"zeroconf": ["_bangolufsen._tcp.local."]
}
40 changes: 39 additions & 1 deletion custom_components/bangolufsen/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
HIDDEN_SOURCE_IDS,
NO_METADATA,
VALID_MEDIA_TYPES,
BEOLINK_RELATIVE_VOLUME,
ArtSizeEnum,
BangOlufsenEntity,
BangOlufsenMediaType,
Expand Down Expand Up @@ -190,6 +191,12 @@ async def async_setup_entry(
func="async_beolink_set_volume",
)

platform.async_register_entity_service(
name="beolink_set_relative_volume",
schema={vol.Required("volume_level"): cv.string},
func="async_beolink_set_relative_volume",
)

platform.async_register_entity_service(
name="beolink_leader_command",
schema={
Expand Down Expand Up @@ -334,6 +341,11 @@ async def async_added_to_hass(self) -> None:
f"{self._beolink_jid}_{BEOLINK_VOLUME}",
self.async_beolink_set_volume,
),
async_dispatcher_connect(
self.hass,
f"{self._beolink_jid}_{BEOLINK_RELATIVE_VOLUME}",
self.async_beolink_set_relative_volume,
),
]
)

Expand Down Expand Up @@ -454,7 +466,7 @@ async def _update_sources(self) -> None:
# Combine the source dicts
self._sources = self._audio_sources | self._video_sources

# HASS won't be necessarily be running the first time this method is run
# HASS won't necessarily be running the first time this method is run
if self.hass.is_running:
self.async_write_ha_state()

Expand Down Expand Up @@ -1212,6 +1224,32 @@ async def async_beolink_set_volume(self, volume_level: str) -> None:
volume_level,
)

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)

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."""

# Get the remote leader to send the volume command to listeners
if self._remote_leader is not None:
async_dispatcher_send(
self.hass,
f"{self._remote_leader.jid}_{BEOLINK_RELATIVE_VOLUME}",
volume_level,
)

else:
await self.async_set_relative_volume_level(volume=float(volume_level))

for beolink_listener in self._beolink_listeners:
async_dispatcher_send(
self.hass,
f"{beolink_listener.jid}_{BEOLINK_LISTENER_COMMAND}",
"set_relative_volume_level",
volume_level,
)

async def async_overlay_audio(
self,
uri: str | None = None,
Expand Down
37 changes: 28 additions & 9 deletions custom_components/bangolufsen/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ beolink_set_volume:
selector:
text:

beolink_set_relative_volume:
name: Beolink set relative volume
description: Set a volume level relative to the current level of all connected Beolink devices.
target:
entity:
integration: bangolufsen
domain: media_player
device:
integration: bangolufsen
fields:
volume_level:
name: Volume level
description: Specify the volume level.
required: true
example: 0.2
selector:
text:

beolink_leader_command:
name: Beolink leader command
description: Send a media_player command to Beolink leader.
Expand All @@ -116,19 +134,20 @@ beolink_leader_command:
selector:
select:
options:
- "set_volume_level"
- "media_seek"
- "mute_volume"
- "select_source"
- "volume_up"
- "volume_down"
- "media_play_pause"
- "media_next_track"
- "media_pause"
- "media_play_pause"
- "media_play"
- "media_stop"
- "media_next_track"
- "media_previous_track"
- "media_seek"
- "media_stop"
- "mute_volume"
- "select_source"
- "set_relative_volume_level"
- "set_volume_level"
- "toggle"
- "volume_down"
- "volume_up"
parameter:
name: Parameter
description: Specify the media_player command's parameter.
Expand Down

0 comments on commit 53e6318

Please sign in to comment.