/
__init__.py
180 lines (137 loc) · 5.35 KB
/
__init__.py
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
"""The SSDP integration."""
import asyncio
from datetime import timedelta
import logging
from urllib.parse import urlparse
import aiohttp
from defusedxml import ElementTree
from netdisco import ssdp, util
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.generated.ssdp import SSDP
DOMAIN = "ssdp"
SCAN_INTERVAL = timedelta(seconds=60)
ATTR_HOST = "host"
ATTR_PORT = "port"
ATTR_SSDP_DESCRIPTION = "ssdp_description"
ATTR_ST = "ssdp_st"
ATTR_NAME = "name"
ATTR_MODEL_NAME = "model_name"
ATTR_MODEL_NUMBER = "model_number"
ATTR_SERIAL = "serial_number"
ATTR_MANUFACTURER = "manufacturer"
ATTR_MANUFACTURERURL = "manufacturerURL"
ATTR_UDN = "udn"
ATTR_UPNP_DEVICE_TYPE = "upnp_device_type"
ATTR_PRESENTATIONURL = "presentation_url"
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass, config):
"""Set up the SSDP integration."""
async def initialize():
scanner = Scanner(hass)
await scanner.async_scan(None)
async_track_time_interval(hass, scanner.async_scan, SCAN_INTERVAL)
hass.loop.create_task(initialize())
return True
class Scanner:
"""Class to manage SSDP scanning."""
def __init__(self, hass):
"""Initialize class."""
self.hass = hass
self.seen = set()
self._description_cache = {}
async def async_scan(self, _):
"""Scan for new entries."""
_LOGGER.debug("Scanning")
# Run 3 times as packets can get lost
for _ in range(3):
entries = await self.hass.async_add_executor_job(ssdp.scan)
await self._process_entries(entries)
# We clear the cache after each run. We track discovered entries
# so will never need a description twice.
self._description_cache.clear()
async def _process_entries(self, entries):
"""Process SSDP entries."""
tasks = []
for entry in entries:
key = (entry.st, entry.location)
if key in self.seen:
continue
self.seen.add(key)
tasks.append(self._process_entry(entry))
if not tasks:
return
to_load = [
result for result in await asyncio.gather(*tasks) if result is not None
]
if not to_load:
return
tasks = []
for entry, info, domains in to_load:
for domain in domains:
_LOGGER.debug("Discovered %s at %s", domain, entry.location)
tasks.append(
self.hass.config_entries.flow.async_init(
domain, context={"source": DOMAIN}, data=info
)
)
await asyncio.wait(tasks)
async def _process_entry(self, entry):
"""Process a single entry."""
info = {"st": entry.st}
if entry.location:
# Multiple entries usually share same location. Make sure
# we fetch it only once.
info_req = self._description_cache.get(entry.location)
if info_req is None:
info_req = self._description_cache[
entry.location
] = self.hass.async_create_task(self._fetch_description(entry.location))
info.update(await info_req)
domains = set()
for domain, matchers in SSDP.items():
for matcher in matchers:
if all(info.get(k) == v for (k, v) in matcher.items()):
domains.add(domain)
if domains:
return (entry, info_from_entry(entry, info), domains)
return None
async def _fetch_description(self, xml_location):
"""Fetch an XML description."""
session = self.hass.helpers.aiohttp_client.async_get_clientsession()
try:
resp = await session.get(xml_location, timeout=5)
xml = await resp.text()
# Samsung Smart TV sometimes returns an empty document the
# first time. Retry once.
if not xml:
resp = await session.get(xml_location, timeout=5)
xml = await resp.text()
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
_LOGGER.debug("Error fetching %s: %s", xml_location, err)
return {}
try:
tree = ElementTree.fromstring(xml)
except ElementTree.ParseError as err:
_LOGGER.debug("Error parsing %s: %s", xml_location, err)
return {}
return util.etree_to_dict(tree).get("root", {}).get("device", {})
def info_from_entry(entry, device_info):
"""Get most important info from an entry."""
url = urlparse(entry.location)
info = {
ATTR_HOST: url.hostname,
ATTR_PORT: url.port,
ATTR_SSDP_DESCRIPTION: entry.location,
ATTR_ST: entry.st,
}
if device_info:
info[ATTR_NAME] = device_info.get("friendlyName")
info[ATTR_MODEL_NAME] = device_info.get("modelName")
info[ATTR_MODEL_NUMBER] = device_info.get("modelNumber")
info[ATTR_SERIAL] = device_info.get("serialNumber")
info[ATTR_MANUFACTURER] = device_info.get("manufacturer")
info[ATTR_MANUFACTURERURL] = device_info.get("manufacturerURL")
info[ATTR_UDN] = device_info.get("UDN")
info[ATTR_UPNP_DEVICE_TYPE] = device_info.get("deviceType")
info[ATTR_PRESENTATIONURL] = device_info.get("presentationURL")
return info