From 4142b1f62fa90b654b00b6a34cd67506f1b7fb90 Mon Sep 17 00:00:00 2001 From: Liborsaf Date: Sun, 7 Apr 2024 17:54:52 +0200 Subject: [PATCH] Add volume level (#36) Improve volume supprt * Add volume level * Change volume debug level * Add volume set support & normalize volume --- .gitignore | 4 +++- custom_components/sony/media_player.py | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index bfbf94f..d10c3ab 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ pylintrc home-assistant.log home-assistant_v2.db test_config/custom_components -test_config/.storage/auth \ No newline at end of file +test_config/.storage/auth + +.idea/ diff --git a/custom_components/sony/media_player.py b/custom_components/sony/media_player.py index 213cb5d..518b992 100644 --- a/custom_components/sony/media_player.py +++ b/custom_components/sony/media_player.py @@ -5,6 +5,8 @@ https://github.com/dilruacs/media_player.sony """ import logging +import time + from sonyapilib.device import SonyDevice import voluptuous as vol @@ -15,7 +17,7 @@ from homeassistant.components.media_player.const import ( SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK, SUPPORT_TURN_ON, SUPPORT_TURN_OFF, SUPPORT_PLAY, SUPPORT_PLAY_MEDIA, SUPPORT_STOP, - SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_STEP) + SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_STEP, SUPPORT_VOLUME_SET) from homeassistant.const import ( CONF_HOST, CONF_NAME, STATE_OFF, STATE_ON, STATE_PLAYING, STATE_PAUSED) @@ -54,7 +56,7 @@ SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \ SUPPORT_TURN_ON | SUPPORT_TURN_OFF | \ SUPPORT_PLAY | SUPPORT_PLAY_MEDIA | SUPPORT_STOP | \ - SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_STEP + SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_STEP | SUPPORT_VOLUME_SET PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_HOST): cv.string, @@ -185,6 +187,7 @@ def __init__(self, sony_device): """ self.sonydevice = sony_device self._state = STATE_OFF + self._attr_volume_level = 0 self._muted = False self._id = None self._playing = False @@ -210,6 +213,7 @@ def update(self): if self._state == STATE_ON: power_status = self.sonydevice.get_power_status() if power_status: + self.update_volume() playback_info = self.sonydevice.get_playing_status() if playback_info == "PLAYING": self._state = STATE_PLAYING @@ -224,6 +228,11 @@ def update(self): _LOGGER.error(exception_instance) self._state = STATE_OFF + def update_volume(self): + """Update volume info.""" + self._attr_volume_level = self.sonydevice.get_volume() / 100 + _LOGGER.debug(self._attr_volume_level) + @property def name(self): """Return the name of the device.""" @@ -297,10 +306,18 @@ def media_stop(self): def volume_up(self): """Send stop command.""" self.sonydevice.volume_up() + time.sleep(0.5) + self.update_volume() def volume_down(self): """Send stop command.""" self.sonydevice.volume_down() + time.sleep(0.5) + self.update_volume() + + def set_volume_level(self, volume): + """Send set volume command.""" + self.sonydevice.set_volume(int(volume * 100)) def mute_volume(self, mute): """Send stop command."""