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
|
"""Module for device notifications."""
import logging
from collections.abc import Awaitable
from pprint import pformat as pf
from typing import Callable, List
import attr
from songpal.containers import PlayInfo, Power, SoftwareUpdateInfo, make
_LOGGER = logging.getLogger(__name__)
class Notification:
"""Wrapper for notifications.
In order to listen for notifications, call `activate(callback)`
with a coroutine to be called when a notification is received.
"""
def __init__(self, endpoint, switch_method, payload):
"""Notification constructor.
:param endpoint: Endpoint.
:param switch_method: `Method` for switching this notification.
:param payload: JSON data containing name and available versions.
"""
self.endpoint = endpoint
self.switch_method = switch_method
self.versions = payload["versions"]
self.name = payload["name"]
self.version = max(x["version"] for x in self.versions if "version" in x)
_LOGGER.debug("notification payload: %s", pf(payload))
def asdict(self):
"""Return a dict containing the notification information."""
return {"name": self.name, "version": self.version}
async def activate(self, callback):
"""Start listening for this notification.
Emits received notifications by calling the passed `callback`.
"""
await self.switch_method({"enabled": [self.asdict()]}, _consumer=callback)
def __repr__(self):
return "<Notification {}, versions={}, endpoint={}>".format(
self.name,
self.versions,
self.endpoint,
)
class ChangeNotification:
"""Dummy base-class for notifications."""
# A type-annotation for change notification callbacks
NotificationCallback = Callable[[ChangeNotification], Awaitable[None]]
@attr.s
class ConnectChange(ChangeNotification):
"""Notification for connection status.
This is used to inform listeners if the device connectivity drops.
"""
connected = attr.ib()
exception = attr.ib(default=None)
@attr.s
class PowerChange(ChangeNotification, Power):
"""Notification for power status change."""
@attr.s
class ZoneActivatedChange(ChangeNotification):
"""Notification for zone power status change."""
make = classmethod(make)
def _convert_bool(x) -> bool:
return x == "active"
active = attr.ib(converter=_convert_bool)
connection = attr.ib()
label = attr.ib()
uri = attr.ib()
@attr.s
class SoftwareUpdateChange(ChangeNotification, SoftwareUpdateInfo):
"""Notification for available software updates."""
@attr.s
class VolumeChange(ChangeNotification):
"""Notification for volume changes."""
make = classmethod(make)
def _convert_bool(x) -> bool:
return x == "on"
mute = attr.ib(converter=_convert_bool)
volume = attr.ib()
output = attr.ib()
@attr.s
class SettingChange(ChangeNotification):
"""Notification for settings change."""
make = classmethod(make)
titleTextID = attr.ib()
guideTextID = attr.ib()
isAvailable = attr.ib()
type = attr.ib()
title = attr.ib()
apiMappingUpdate = attr.ib()
target = attr.ib()
currentValue = attr.ib()
def __attrs_post_init__(self):
if self.type == "directory":
_LOGGER.debug(
"Got SettingChange for directory %s (see #36), " "ignoring..",
self.titleTextID,
)
return
if self.apiMappingUpdate is None:
_LOGGER.warning(
"Got SettingChange for %s without " "apiMappingUpdate, ignoring..",
self.titleTextID,
)
return
self.currentValue = self.apiMappingUpdate["currentValue"]
self.target = self.apiMappingUpdate["target"]
def __str__(self):
return "<SettingChange {} {} ({}): {}>".format(
self.title,
self.type,
self.target,
self.currentValue,
)
@attr.s
class ContentChange(ChangeNotification, PlayInfo):
"""This gets sent as a notification when the source changes."""
make = classmethod(make)
kind = attr.ib()
"""Used by newer devices, continue to access via `contentKind`"""
def __attrs_post_init__(self):
if self.contentKind is None:
self.contentKind = self.kind
@property
def is_input(self):
"""Return if the change was related to input."""
return self.contentKind == "input"
@attr.s
class NotificationChange(ChangeNotification):
"""Container for storing information about state of Notifications."""
make = classmethod(make)
def _extract_notification_names(x) -> List[str]:
return [x["name"] for x in x] # type: ignore
enabled = attr.ib(converter=_extract_notification_names)
disabled = attr.ib(converter=_extract_notification_names)
def __str__(self):
return "<NotificationChange enabled: {} disabled: {}>".format(
",".join(self.enabled),
",".join(self.disabled),
)
@attr.s
class PlaybackFunctionChange(ChangeNotification):
"""Container for storing playback function changes."""
make = classmethod(make)
functions = attr.ib()
uri = attr.ib()
@attr.s
class StorageChange(ChangeNotification):
"""Container for storing storage changes."""
make = classmethod(make)
isAvailable = attr.ib()
mounted = attr.ib()
uri = attr.ib()
|