1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
"""
This module provides an interface to control device groups..
In contrast to most of the features in the library, the groups are controlled using
UPnP. This class implements urn:schemas-sony-com:service:Group:1 UPnP service.
"""
import logging
import attr
from async_upnp_client.aiohttp import AiohttpRequester
from async_upnp_client.client_factory import UpnpFactory
from .containers import make
_LOGGER = logging.getLogger(__name__)
@attr.s
class GroupState:
"""Container for group state information."""
make = classmethod(make)
"""{'Discoverable': 'NO',
'GroupMode': 'IDLE',
'GroupName': '',
'GroupSong': 'PUBLIC',
'GroupState': 'IDLE',
'MasterSessionID': 0,
'MasterUUID': '',
'NumberOfSlaves': 0,
'PlayingState': 'STOPPED',
'PowerState': 'ON',
'RSSIValue': -46,
'SessionID': 0,
'SlaveList': '',
'SlaveNetworkState': '',
'WiredLinkSpeed': 0,
'WiredState': 'DOWN',
'WirelessLinkSpeed': 65,
'WirelessState': 'UP',
'WirelessType': '802.11bgn'}
"""
Discoverable = attr.ib()
GroupMode = attr.ib()
GroupName = attr.ib()
GroupSong = attr.ib()
GroupState = attr.ib()
MasterSessionID = attr.ib()
MasterUUID = attr.ib()
NumberOfSlaves = attr.ib()
PlayingState = attr.ib()
PowerState = attr.ib()
RSSIValue = attr.ib()
SessionID = attr.ib()
SlaveList = attr.ib()
SlaveNetworkState = attr.ib()
WiredLinkSpeed = attr.ib()
WiredState = attr.ib()
WirelessLinkSpeed = attr.ib()
WirelessState = attr.ib()
WirelessType = attr.ib()
# For GetStateM
GroupMemoryCount = attr.ib(default=None)
GroupMemoryUpdateID = attr.ib(default=None)
def __str__(self):
s = "Power: %s" % self.PowerState
s += "\nMode: %s" % self.GroupMode
if self.GroupMode == "GROUP":
s += "\nSession ID: %s" % self.SessionID
s += "\nGroup: %s" % self.GroupName
s += "\nState: %s" % self.GroupState
s += "\nSlaves: %s" % self.NumberOfSlaves
s += "\n %s" % self.SlaveList
if self.WiredState != "DOWN":
s += "\nConnection: Wired"
if self.WirelessState != "DOWN":
s += "\nConnection: %s" % self.WirelessType
return s
class GroupControl:
"""Class for controlling speaker groups.
This provides an interface to control device groups
using UPnP interface 'urn:schemas-sony-com:service:Group:1'.
"""
def __init__(self, url):
self.url = url
async def connect(self):
"""Connect and initialize the controls.
Returns False if the UPnP service is not found.
"""
requester = AiohttpRequester()
factory = UpnpFactory(requester)
device = await factory.async_create_device(self.url)
self.service = device.service("urn:schemas-sony-com:service:Group:1")
if not self.service:
_LOGGER.error("Unable to find group service!")
return False
for act in self.service.actions.values():
_LOGGER.debug(
"Action: %s (%s)", act, [arg.name for arg in act.in_arguments()]
)
return True
"""
Available actions
<UpnpService.Action(X_GetDeviceInfo)> ([])
<UpnpService.Action(X_GetState)> ([])
<UpnpService.Action(X_GetStateM)> ([])
<UpnpService.Action(X_SetGroupName)> (['GroupName'])
<UpnpService.Action(X_ChangeGroupVolume)> (['GroupVolume'])
<UpnpService.Action(X_GetAllGroupMemory)> ([])
<UpnpService.Action(X_DeleteGroupMemory)> (['MemoryID'])
<UpnpService.Action(X_UpdateGroupMemory)> (['MemoryID', 'GroupMode',
'GroupName', 'SlaveList', 'CodecType', 'CodecBitrate'])
<UpnpService.Action(X_Start)> (['GroupMode', 'GroupName', 'SlaveList',
'CodecType', 'CodecBitrate'])
<UpnpService.Action(X_Entry)> (['MasterSessionID', 'SlaveList'])
<UpnpService.Action(X_EntryM)> (['MasterSessionID', 'SlaveList'])
<UpnpService.Action(X_Leave)> (['MasterSessionID', 'SlaveList'])
<UpnpService.Action(X_LeaveM)> (['MasterSessionID', 'SlaveList'])
<UpnpService.Action(X_Abort)> (['MasterSessionID'])
<UpnpService.Action(X_SetGroupMute)> (['GroupMute'])
<UpnpService.Action(X_SetCodec)> (['CodecType', 'CodecBitrate'])
<UpnpService.Action(X_GetCodec)> ([])
<UpnpService.Action(X_Invite)> (['GroupMode', 'GroupName', 'MasterUUID',
'MasterSessionID'])
<UpnpService.Action(X_Exit)> (['SlaveSessionID'])
<UpnpService.Action(X_Play)> (['MasterSessionID'])
<UpnpService.Action(X_Stop)> (['MasterSessionID'])
<UpnpService.Action(X_Delegate)> (['GroupMode', 'SlaveList', 'DelegateURI',
'DelegateURIMetaData'])
"""
async def call(self, action, **kwargs):
"""Make an action call with given kwargs."""
act = self.service.action(action)
_LOGGER.info("Calling %s with %s", action, kwargs)
res = await act.async_call(**kwargs)
_LOGGER.info(" Result: %s" % res)
return res
async def info(self):
"""Return device info."""
"""
{'MasterCapability': 9, 'TransportPort': 3975}
"""
act = self.service.action("X_GetDeviceInfo")
res = await act.async_call()
return res
async def state(self) -> GroupState:
"""Return the current group state."""
act = self.service.action("X_GetState")
res = await act.async_call()
return GroupState.make(**res)
async def statem(self) -> GroupState:
"""Return the current group state (memory?)."""
act = self.service.action("X_GetStateM")
res = await act.async_call()
return GroupState.make(**res)
async def get_group_memory(self):
"""Return group memory."""
# Returns an XML with groupMemoryList
act = self.service.action("X_GetAllGroupMemory")
res = await act.async_call()
return res
async def update_group_memory(
self, memory_id, mode, name, slaves, codectype=0x0040, bitrate=0x0003
):
"""Update existing memory.
Unknown if this can be used to create new ones, too.
"""
act = self.service.action("X_UpdateGroupMemory")
res = await act.async_call(
MemoryID=memory_id,
GroupMode=mode,
GroupName=name,
SlaveList=slaves,
CodecType=codectype,
CodecBitrate=bitrate,
)
return res
async def delete_group_memory(self, memory_id):
"""Delete group memory."""
act = self.service.action("X_DeleteGroupMemory")
return await act.async_call(MemoryID=memory_id)
async def get_codec(self):
"""Get codec settings."""
act = self.service.action("X_GetCodec")
res = await act.async_call()
return res
async def set_codec(self, codectype=0x0040, bitrate=0x0003):
"""Set codec settings."""
act = self.service.action("X_SetCodec")
res = await act.async_call(CodecType=codectype, CodecBitrate=bitrate)
return res
async def abort(self):
"""Abort current group session."""
state = await self.state()
res = await self.call("X_Abort", MasterSessionID=state.SessionID)
return res
async def stop(self):
"""Stop playback."""
state = await self.state()
res = await self.call("X_Stop", MasterSessionID=state.SessionID)
return res
async def play(self):
"""Start playback."""
state = await self.state()
res = await self.call("X_Play", MasterSessionID=state.SessionID)
return res
async def create(self, name, slaves):
"""Create a group."""
# NOTE: codectype and codecbitrate were simply chosen from an example..
res = await self.call(
"X_Start",
GroupMode="GROUP",
GroupName=name,
SlaveList=",".join(slaves),
CodecType=0x0040,
CodecBitrate=0x0003,
)
return res
async def add(self, slaves):
"""Add slaves to the current group."""
state = await self.state()
res = await self.call(
"X_Entry", MasterSessionID=state.SessionID, SlaveList=slaves
)
return res
async def add_m(self, slaves):
"""Unknown usage."""
state = await self.state()
return await self.call(
"X_EntryM", MasterSessionID=state.SessionID, SlaveList=slaves
)
async def remove(self, slaves):
"""Remove slaves from the current group."""
state = await self.state()
return await self.call(
"X_Leave", MasterSessionID=state.SessionID, SlaveList=slaves
)
async def remove_m(self, slaves):
"""Unknown usage."""
state = await self.state()
return await self.call(
"X_LeaveM", MasterSessionID=state.SessionID, SlaveList=slaves
)
async def set_mute(self, activate):
"""Set group mute."""
res = await self.call("X_SetGroupMute", GroupMute=activate)
return res
async def set_group_volume(self, volume):
"""Set group volume."""
res = await self.call("X_ChangeGroupVolume", GroupVolume=volume)
return res
async def set_group_name(self, name):
"""Set group name."""
res = await self.call("X_SetGroupName", GroupName=name)
return res
|