Skip to content

Commit

Permalink
Add volume validation, accept 0-100 and 0.0-1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlawren committed Nov 30, 2021
1 parent da90a00 commit 17ab0ae
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ data:
media_content_id: https://<unique_cloud_id>.ui.nabu.casa/local/sound_files/doorbell.mp3
media_content_type: music
extra:
volume: 35
volume: 35 # Can be provided as 0-100 or 0.0-1.0
```

A special `media_content_id` of "CHIME" can be used to test the integration using the built-in sound provided by Sonos. This can be useful for validation if your own URLs are not playing correctly:
Expand Down
10 changes: 9 additions & 1 deletion custom_components/sonos_cloud/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_IDLE
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN, PLAYERS, SESSION
Expand Down Expand Up @@ -65,7 +66,14 @@ async def async_play_media(
_LOGGER.info("Playing %s", media_id)
if extra := kwargs.get(ATTR_MEDIA_EXTRA):
if volume := extra.get(ATTR_VOLUME):
data[ATTR_VOLUME] = volume
_LOGGER.info("Type of %s: %s", volume, type(volume))
if type(volume) not in (int, float):
raise HomeAssistantError(f"Volume '{volume}' not a number")
if not 0 < volume <= 100:
raise HomeAssistantError(f"Volume '{volume}' not in acceptable range of 0-100")
if volume < 1:
volume = volume * 100
data[ATTR_VOLUME] = int(volume)
if media_id != "CHIME":
data["streamUrl"] = media_id
session = self.hass.data[DOMAIN][SESSION]
Expand Down

0 comments on commit 17ab0ae

Please sign in to comment.