Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set volume function #71

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions sonyapilib/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,11 +758,13 @@ def get_playing_status(self):
return "OFF"
return find_in_xml(content, [".//CurrentTransportState"]).text

def get_volume(self):
def get_volume(self, channel=None, instance_id=0):
"""Get device volume."""
data = """<m:GetVolume xmlns:m="urn:schemas-upnp-org:service:RenderingControl:1">
<InstanceID>0</InstanceID>
<Channel>Master</Channel>
channel = channel or "Master"

data = f"""<m:GetVolume xmlns:m="urn:schemas-upnp-org:service:RenderingControl:1">
<InstanceID>{instance_id}</InstanceID>
<Channel>{channel}</Channel>
</m:GetVolume>"""

action = "urn:schemas-upnp-org:service:RenderingControl:1#GetVolume"
Expand All @@ -775,6 +777,26 @@ def get_volume(self):

return int(find_in_xml(content, [".//CurrentVolume"]).text)

def set_volume(self, volume, channel=None, instance_id=0):
"""Set device volume."""
channel = channel or "Master"

data = f"""<m:SetVolume xmlns:m="urn:schemas-upnp-org:service:RenderingControl:1">
<InstanceID>{instance_id}</InstanceID>
<Channel>{channel}</Channel>
<DesiredVolume>{volume}</DesiredVolume>
</m:SetVolume>"""

action = "urn:schemas-upnp-org:service:RenderingControl:1#SetVolume"

content = self._post_soap_request(
url=self.rendering_control_url, params=data, action=action)

if not content:
return False

return "SetVolumeResponse" in content

def get_power_status(self):
"""Check if the device is online."""
if self.api_version < 4:
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions tests/data/set_volume.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:SetVolumeResponse xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"></u:SetVolumeResponse>
</s:Body>
</s:Envelope>
25 changes: 20 additions & 5 deletions tests/device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
BASE_URL = 'http://test/sony'
AV_TRANSPORT_URL = 'http://test:52323/upnp/control/AVTransport'
AV_TRANSPORT_URL_NO_MEDIA = 'http://test2:52323/upnp/control/AVTransport'
RENDERING_CONTROL_URL = 'http://test:52323/upnp/control/RenderingControl'
RENDERING_CONTROL_URL_GET_VOLUME = 'http://test:52323/upnp/control/RenderingControl'
RENDERING_CONTROL_URL_SET_VOLUME = 'http://test2:52323/upnp/control/RenderingControl'
REQUESTS_ERROR = 'http://ERROR'

ACTION_LIST = [
Expand Down Expand Up @@ -169,11 +170,17 @@ def mocked_requests_post(*args, **kwargs):
read_file(
'data/playing_status_legacy_no_media.xml'))

elif url == RENDERING_CONTROL_URL:
elif url == RENDERING_CONTROL_URL_GET_VOLUME:
return MockResponse(None,
200,
read_file(
'data/volume.xml'))
'data/get_volume.xml'))

elif url == RENDERING_CONTROL_URL_SET_VOLUME:
return MockResponse(None,
200,
read_file(
'data/set_volume.xml'))

elif url == COMMAND_LIST_V4:
json_data = jsonpickle.decode(read_file('data/commandList.json'))
Expand Down Expand Up @@ -857,13 +864,21 @@ def test_playing_status_no_media_legacy(self, mocked_requests_post):
self.assertEqual("PLAYING", device.get_playing_status())

@mock.patch('requests.post', side_effect=mocked_requests_post)
def test_volume(self, mocked_requests_post):
def test_get_volume(self, mocked_requests_post):
device = self.create_device()
self.assertEqual(-1, device.get_volume())

device.rendering_control_url = RENDERING_CONTROL_URL
device.rendering_control_url = RENDERING_CONTROL_URL_GET_VOLUME
self.assertEqual(64, device.get_volume())

@mock.patch('requests.post', side_effect=mocked_requests_post)
def test_set_volume(self, mocked_requests_post):
device = self.create_device()

device.rendering_control_url = RENDERING_CONTROL_URL_SET_VOLUME
self.assertEqual(True, device.set_volume(50))
self.assertEqual(True, device.set_volume(0))

def test_irrc_is_dmr(self):
dev = SonyDevice(host="none", nickname="none", ircc_port=42, dmr_port=42)
self.assertEqual(dev.dmr_url, dev.ircc_url)
Expand Down
Loading