Skip to content

Commit

Permalink
Add volume level (#36)
Browse files Browse the repository at this point in the history
Improve volume supprt

* Add volume level

* Change volume debug level

* Add volume set support & normalize volume
  • Loading branch information
Liborsaf committed Apr 7, 2024
1 parent df1c216 commit 4142b1f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ pylintrc
home-assistant.log
home-assistant_v2.db
test_config/custom_components
test_config/.storage/auth
test_config/.storage/auth

.idea/
21 changes: 19 additions & 2 deletions custom_components/sony/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
https://github.com/dilruacs/media_player.sony
"""
import logging
import time

from sonyapilib.device import SonyDevice

import voluptuous as vol
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 4142b1f

Please # to comment.