Fix snapcast uuid to be more unique (#14925)

Current uuid is ok when using only 1 snapserver
New uuid is needed when using multiple snapserver

Because the client can connect to more snapservers and
then uuid based on client MAC is not enough
This commit is contained in:
Ing. Jaroslav Šafka 2018-06-12 15:46:53 +02:00 committed by Martin Hjelmare
parent 6755ae2605
commit 89d008d1f3

View File

@ -80,8 +80,11 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
host, port) host, port)
return return
groups = [SnapcastGroupDevice(group) for group in server.groups] # Note: Host part is needed, when using multiple snapservers
clients = [SnapcastClientDevice(client) for client in server.clients] hpid = '{}:{}'.format(host, port)
groups = [SnapcastGroupDevice(group, hpid) for group in server.groups]
clients = [SnapcastClientDevice(client, hpid) for client in server.clients]
devices = groups + clients devices = groups + clients
hass.data[DATA_KEY] = devices hass.data[DATA_KEY] = devices
async_add_devices(devices) async_add_devices(devices)
@ -90,10 +93,12 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
class SnapcastGroupDevice(MediaPlayerDevice): class SnapcastGroupDevice(MediaPlayerDevice):
"""Representation of a Snapcast group device.""" """Representation of a Snapcast group device."""
def __init__(self, group): def __init__(self, group, uid_part):
"""Initialize the Snapcast group device.""" """Initialize the Snapcast group device."""
group.set_callback(self.schedule_update_ha_state) group.set_callback(self.schedule_update_ha_state)
self._group = group self._group = group
self._uid = '{}{}_{}'.format(GROUP_PREFIX, uid_part,
self._group.identifier)
@property @property
def state(self): def state(self):
@ -107,7 +112,7 @@ class SnapcastGroupDevice(MediaPlayerDevice):
@property @property
def unique_id(self): def unique_id(self):
"""Return the ID of snapcast group.""" """Return the ID of snapcast group."""
return '{}{}'.format(GROUP_PREFIX, self._group.identifier) return self._uid
@property @property
def name(self): def name(self):
@ -185,15 +190,21 @@ class SnapcastGroupDevice(MediaPlayerDevice):
class SnapcastClientDevice(MediaPlayerDevice): class SnapcastClientDevice(MediaPlayerDevice):
"""Representation of a Snapcast client device.""" """Representation of a Snapcast client device."""
def __init__(self, client): def __init__(self, client, uid_part):
"""Initialize the Snapcast client device.""" """Initialize the Snapcast client device."""
client.set_callback(self.schedule_update_ha_state) client.set_callback(self.schedule_update_ha_state)
self._client = client self._client = client
self._uid = '{}{}_{}'.format(CLIENT_PREFIX, uid_part,
self._client.identifier)
@property @property
def unique_id(self): def unique_id(self):
"""Return the ID of this snapcast client.""" """
return '{}{}'.format(CLIENT_PREFIX, self._client.identifier) Return the ID of this snapcast client.
Note: Host part is needed, when using multiple snapservers
"""
return self._uid
@property @property
def name(self): def name(self):