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
|
# -*- coding: utf-8 -*-
"""async_upnp_client.const module."""
from dataclasses import dataclass
from datetime import date, datetime, time
from enum import Enum
from ipaddress import IPv4Address, IPv6Address
from typing import (
Any,
Callable,
List,
Mapping,
MutableMapping,
NamedTuple,
Optional,
Tuple,
Union,
)
from xml.etree import ElementTree as ET
from async_upnp_client.utils import parse_date_time, require_tzinfo
IPvXAddress = Union[IPv4Address, IPv6Address] # pylint: disable=invalid-name
AddressTupleV4Type = Tuple[str, int]
AddressTupleV6Type = Tuple[str, int, int, int]
AddressTupleVXType = Union[ # pylint: disable=invalid-name
AddressTupleV4Type, AddressTupleV6Type
]
NS = {
"soap_envelope": "http://schemas.xmlsoap.org/soap/envelope/",
"device": "urn:schemas-upnp-org:device-1-0",
"service": "urn:schemas-upnp-org:service-1-0",
"event": "urn:schemas-upnp-org:event-1-0",
"control": "urn:schemas-upnp-org:control-1-0",
}
MIME_TO_UPNP_CLASS_MAPPING: Mapping[str, str] = {
"audio": "object.item.audioItem",
"video": "object.item.videoItem",
"image": "object.item.imageItem",
"application/dash+xml": "object.item.videoItem",
"application/x-mpegurl": "object.item.videoItem",
"application/vnd.apple.mpegurl": "object.item.videoItem",
}
STATE_VARIABLE_TYPE_MAPPING: Mapping[str, Mapping[str, Callable]] = {
"ui1": {"type": int, "in": int, "out": str},
"ui2": {"type": int, "in": int, "out": str},
"ui4": {"type": int, "in": int, "out": str},
"ui8": {"type": int, "in": int, "out": str},
"i1": {"type": int, "in": int, "out": str},
"i2": {"type": int, "in": int, "out": str},
"i4": {"type": int, "in": int, "out": str},
"i8": {"type": int, "in": int, "out": str},
"int": {"type": int, "in": int, "out": str},
"r4": {"type": float, "in": float, "out": str},
"r8": {"type": float, "in": float, "out": str},
"number": {"type": float, "in": float, "out": str},
"fixed.14.4": {"type": float, "in": float, "out": str},
"float": {"type": float, "in": float, "out": str},
"char": {"type": str, "in": str, "out": str},
"string": {"type": str, "in": str, "out": str},
"boolean": {
"type": bool,
"in": lambda s: s.lower() in ["1", "true", "yes"],
"out": lambda b: "1" if b else "0",
},
"bin.base64": {"type": str, "in": str, "out": str},
"bin.hex": {"type": str, "in": str, "out": str},
"uri": {"type": str, "in": str, "out": str},
"uuid": {"type": str, "in": str, "out": str},
"date": {"type": date, "in": parse_date_time, "out": lambda d: d.isoformat()},
"dateTime": {
"type": datetime,
"in": parse_date_time,
"out": lambda dt: dt.isoformat("T", "seconds"),
},
"dateTime.tz": {
"type": datetime,
"validator": require_tzinfo,
"in": parse_date_time,
"out": lambda dt: dt.isoformat("T", "seconds"),
},
"time": {
"type": time,
"in": parse_date_time,
"out": lambda t: t.isoformat("seconds"),
},
"time.tz": {
"type": time,
"validator": require_tzinfo,
"in": parse_date_time,
"out": lambda t: t.isoformat("T", "seconds"),
},
}
class DeviceIcon(NamedTuple):
"""Device icon."""
mimetype: str
width: int
height: int
depth: int
url: str
class DeviceInfo(NamedTuple):
"""Device info."""
device_type: str
friendly_name: str
manufacturer: str
manufacturer_url: Optional[str]
model_description: Optional[str]
model_name: str
model_number: Optional[str]
model_url: Optional[str]
serial_number: Optional[str]
udn: str
upc: Optional[str]
presentation_url: Optional[str]
url: str
icons: List[DeviceIcon]
xml: ET.Element
class ServiceInfo(NamedTuple):
"""Service info."""
service_id: str
service_type: str
control_url: str
event_sub_url: str
scpd_url: str
xml: ET.Element
class ActionArgumentInfo(NamedTuple):
"""Action argument info."""
name: str
direction: str
state_variable_name: str
xml: ET.Element
class ActionInfo(NamedTuple):
"""Action info."""
name: str
arguments: List[ActionArgumentInfo]
xml: ET.Element
@dataclass(frozen=True)
class HttpRequest:
"""HTTP request."""
method: str
url: str
headers: Mapping[str, str]
body: Optional[str]
@dataclass(frozen=True)
class HttpResponse:
"""HTTP response."""
status_code: int
headers: Mapping[str, str]
body: Optional[str]
@dataclass(frozen=True)
class StateVariableTypeInfo:
"""State variable type info."""
data_type: str
data_type_mapping: Mapping[str, Callable]
default_value: Optional[str]
allowed_value_range: Mapping[str, Optional[str]]
allowed_values: Optional[List[str]]
xml: ET.Element
@dataclass(frozen=True)
class EventableStateVariableTypeInfo(StateVariableTypeInfo):
"""Eventable State variable type info."""
max_rate: Optional[float]
@dataclass(frozen=True)
class StateVariableInfo:
"""State variable info."""
name: str
send_events: bool
type_info: StateVariableTypeInfo
xml: ET.Element
# Headers
SsdpHeaders = MutableMapping[str, Any]
NotificationType = str # NT header
UniqueServiceName = str # USN header
SearchTarget = str # ST header
UniqueDeviceName = str # UDN
DeviceOrServiceType = str
# Event handler
ServiceId = str # SID
class NotificationSubType(str, Enum):
"""NTS header."""
SSDP_ALIVE = "ssdp:alive"
SSDP_BYEBYE = "ssdp:byebye"
SSDP_UPDATE = "ssdp:update"
class SsdpSource(str, Enum):
"""SSDP source."""
ADVERTISEMENT = "advertisement"
SEARCH = "search"
# More detailed
SEARCH_ALIVE = "search_alive"
SEARCH_CHANGED = "search_changed"
# More detailed.
ADVERTISEMENT_ALIVE = "advertisement_alive"
ADVERTISEMENT_BYEBYE = "advertisement_byebye"
ADVERTISEMENT_UPDATE = "advertisement_update"
|