Skip to content

Commit

Permalink
Merge pull request #53 from luar123/fix_test
Browse files Browse the repository at this point in the history
Fix Python 3.11
  • Loading branch information
happyleavesaoc authored Mar 6, 2023
2 parents d209915 + 046f5cb commit 846337b
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 133 deletions.
6 changes: 2 additions & 4 deletions snapcast/control/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
"""Snapcast control for Snapcast 0.11.1."""

import asyncio
from snapcast.control.server import Snapserver, CONTROL_PORT


@asyncio.coroutine
def create_server(loop, host, port=CONTROL_PORT, reconnect=False):
async def create_server(loop, host, port=CONTROL_PORT, reconnect=False):
"""Server factory."""
server = Snapserver(loop, host, port, reconnect)
yield from server.start()
await server.start()
return server
39 changes: 16 additions & 23 deletions snapcast/control/client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""Snapcast client."""
import asyncio
import logging


_LOGGER = logging.getLogger(__name__)


# pylint: disable=too-many-public-methods
class Snapclient(object):
class Snapclient():
"""Represents a snapclient."""

def __init__(self, server, data):
Expand Down Expand Up @@ -56,60 +55,56 @@ def name(self):
"""Name."""
return self._client.get('config').get('name')

@asyncio.coroutine
def set_name(self, name):
async def set_name(self, name):
"""Set a client name."""
if not name:
name = ''
self._client['config']['name'] = name
yield from self._server.client_name(self.identifier, name)
await self._server.client_name(self.identifier, name)

@property
def latency(self):
"""Latency."""
return self._client.get('config').get('latency')

@asyncio.coroutine
def set_latency(self, latency):
async def set_latency(self, latency):
"""Set client latency."""
self._client['config']['latency'] = latency
yield from self._server.client_latency(self.identifier, latency)
await self._server.client_latency(self.identifier, latency)

@property
def muted(self):
"""Muted or not."""
return self._client.get('config').get('volume').get('muted')

@asyncio.coroutine
def set_muted(self, status):
async def set_muted(self, status):
"""Set client mute status."""
new_volume = self._client['config']['volume']
new_volume['muted'] = status
self._client['config']['volume']['muted'] = status
yield from self._server.client_volume(self.identifier, new_volume)
await self._server.client_volume(self.identifier, new_volume)
_LOGGER.info('set muted to %s on %s', status, self.friendly_name)

@property
def volume(self):
"""Volume percent."""
return self._client.get('config').get('volume').get('percent')

@asyncio.coroutine
def set_volume(self, percent, update_group=True):
async def set_volume(self, percent, update_group=True):
"""Set client volume percent."""
if percent not in range(0, 101):
raise ValueError('Volume percent out of range')
new_volume = self._client['config']['volume']
new_volume['percent'] = percent
self._client['config']['volume']['percent'] = percent
yield from self._server.client_volume(self.identifier, new_volume)
await self._server.client_volume(self.identifier, new_volume)
if update_group:
self._server.group(self.group.identifier).callback()
_LOGGER.info('set volume to %s on %s', percent, self.friendly_name)

def groups_available(self):
"""Get available group objects."""
return [group for group in self._server.groups]
return list(self._server.groups)

def update_volume(self, data):
"""Update volume."""
Expand Down Expand Up @@ -146,15 +141,14 @@ def snapshot(self):
}
_LOGGER.info('took snapshot of current state of %s', self.friendly_name)

@asyncio.coroutine
def restore(self):
async def restore(self):
"""Restore snapshotted state."""
if not self._snapshot:
return
yield from self.set_name(self._snapshot['name'])
yield from self.set_volume(self._snapshot['volume'])
yield from self.set_muted(self._snapshot['muted'])
yield from self.set_latency(self._snapshot['latency'])
await self.set_name(self._snapshot['name'])
await self.set_volume(self._snapshot['volume'])
await self.set_muted(self._snapshot['muted'])
await self.set_latency(self._snapshot['latency'])
self.callback()
_LOGGER.info('restored snapshot of state of %s', self.friendly_name)

Expand All @@ -169,5 +163,4 @@ def set_callback(self, func):

def __repr__(self):
"""String representation."""
return 'Snapclient {} ({}, {})'.format(
self.version, self.friendly_name, self.identifier)
return f'Snapclient {self.version} ({self.friendly_name}, {self.identifier})'
48 changes: 20 additions & 28 deletions snapcast/control/group.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""Snapcast group."""
import asyncio
import logging


_LOGGER = logging.getLogger(__name__)


# pylint: disable=too-many-public-methods
class Snapgroup(object):
class Snapgroup():
"""Represents a snapcast group."""

def __init__(self, server, data):
Expand All @@ -31,24 +30,22 @@ def name(self):
"""Get group name."""
return self._group.get('name')

@asyncio.coroutine
def set_name(self, name):
async def set_name(self, name):
"""Set a group name."""
if not name:
name = ''
self._group['name'] = name
yield from self._server.group_name(self.identifier, name)
await self._server.group_name(self.identifier, name)

@property
def stream(self):
"""Get stream identifier."""
return self._group.get('stream_id')

@asyncio.coroutine
def set_stream(self, stream_id):
async def set_stream(self, stream_id):
"""Set group stream."""
self._group['stream_id'] = stream_id
yield from self._server.group_stream(self.identifier, stream_id)
await self._server.group_stream(self.identifier, stream_id)
_LOGGER.info('set stream to %s on %s', stream_id, self.friendly_name)

@property
Expand All @@ -61,11 +58,10 @@ def muted(self):
"""Get mute status."""
return self._group.get('muted')

@asyncio.coroutine
def set_muted(self, status):
async def set_muted(self, status):
"""Set group mute status."""
self._group['muted'] = status
yield from self._server.group_mute(self.identifier, status)
await self._server.group_mute(self.identifier, status)
_LOGGER.info('set muted to %s on %s', status, self.friendly_name)

@property
Expand All @@ -76,8 +72,7 @@ def volume(self):
volume_sum += self._server.client(client.get('id')).volume
return int(volume_sum / len(self._group.get('clients')))

@asyncio.coroutine
def set_volume(self, volume):
async def set_volume(self, volume):
"""Set volume."""
if volume not in range(0, 101):
raise ValueError('Volume out of range')
Expand All @@ -98,7 +93,7 @@ def set_volume(self, volume):
else:
client_volume += ratio * (100 - client_volume)
client_volume = round(client_volume)
yield from client.set_volume(client_volume, update_group=False)
await client.set_volume(client_volume, update_group=False)
client.update_volume({
'volume': {
'percent': client_volume,
Expand All @@ -117,29 +112,27 @@ def clients(self):
"""Get client identifiers."""
return [client.get('id') for client in self._group.get('clients')]

@asyncio.coroutine
def add_client(self, client_identifier):
async def add_client(self, client_identifier):
"""Add a client."""
if client_identifier in self.clients:
_LOGGER.error('%s already in group %s', client_identifier, self.identifier)
return
new_clients = self.clients
new_clients.append(client_identifier)
yield from self._server.group_clients(self.identifier, new_clients)
await self._server.group_clients(self.identifier, new_clients)
_LOGGER.info('added %s to %s', client_identifier, self.identifier)
status = yield from self._server.status()
status = await self._server.status()
self._server.synchronize(status)
self._server.client(client_identifier).callback()
self.callback()

@asyncio.coroutine
def remove_client(self, client_identifier):
async def remove_client(self, client_identifier):
"""Remove a client."""
new_clients = self.clients
new_clients.remove(client_identifier)
yield from self._server.group_clients(self.identifier, new_clients)
await self._server.group_clients(self.identifier, new_clients)
_LOGGER.info('removed %s from %s', client_identifier, self.identifier)
status = yield from self._server.status()
status = await self._server.status()
self._server.synchronize(status)
self._server.client(client_identifier).callback()
self.callback()
Expand Down Expand Up @@ -175,14 +168,13 @@ def snapshot(self):
}
_LOGGER.info('took snapshot of current state of %s', self.friendly_name)

@asyncio.coroutine
def restore(self):
async def restore(self):
"""Restore snapshotted state."""
if not self._snapshot:
return
yield from self.set_muted(self._snapshot['muted'])
yield from self.set_volume(self._snapshot['volume'])
yield from self.set_stream(self._snapshot['stream'])
await self.set_muted(self._snapshot['muted'])
await self.set_volume(self._snapshot['volume'])
await self.set_stream(self._snapshot['stream'])
self.callback()
_LOGGER.info('restored snapshot of state of %s', self.friendly_name)

Expand All @@ -197,4 +189,4 @@ def set_callback(self, func):

def __repr__(self):
"""String representation."""
return 'Snapgroup ({}, {})'.format(self.friendly_name, self.identifier)
return f'Snapgroup ({self.friendly_name}, {self.identifier})'
9 changes: 4 additions & 5 deletions snapcast/control/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def connection_made(self, transport):
"""When a connection is made."""
self._transport = transport

def connection_lost(self, exception):
def connection_lost(self, exc):
"""When a connection is lost."""
self._callbacks.get(SERVER_ONDISCONNECT)(exception)
self._callbacks.get(SERVER_ONDISCONNECT)(exc)

def data_received(self, data):
"""Handle received data."""
Expand Down Expand Up @@ -68,13 +68,12 @@ def handle_notification(self, data):
if data.get('method') in self._callbacks:
self._callbacks.get(data.get('method'))(data.get('params'))

@asyncio.coroutine
def request(self, method, params):
async def request(self, method, params):
"""Send a JSONRPC request."""
identifier = random.randint(1, 1000)
self._transport.write(jsonrpc_request(method, identifier, params))
self._buffer[identifier] = {'flag': asyncio.Event()}
yield from self._buffer[identifier]['flag'].wait()
await self._buffer[identifier]['flag'].wait()
result = self._buffer[identifier]['data']
error = self._buffer[identifier]['error']
del self._buffer[identifier]['data']
Expand Down
Loading

0 comments on commit 846337b

Please sign in to comment.