2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Swisscom routers (Internet-Box)."""
|
2021-03-23 13:36:43 +00:00
|
|
|
from contextlib import suppress
|
2016-11-05 20:04:44 +00:00
|
|
|
import logging
|
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
from aiohttp.hdrs import CONTENT_TYPE
|
2016-11-05 20:04:44 +00:00
|
|
|
import requests
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
from homeassistant.components.device_tracker import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
2021-05-21 11:08:40 +00:00
|
|
|
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
2019-07-31 19:25:30 +00:00
|
|
|
DeviceScanner,
|
|
|
|
)
|
2016-11-05 20:04:44 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
2017-11-04 19:04:05 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-11-05 20:04:44 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_IP = "192.168.1.1"
|
2016-11-05 20:04:44 +00:00
|
|
|
|
2021-05-21 11:08:40 +00:00
|
|
|
PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
|
2019-07-31 19:25:30 +00:00
|
|
|
{vol.Optional(CONF_HOST, default=DEFAULT_IP): cv.string}
|
|
|
|
)
|
2016-11-05 20:04:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_scanner(hass, config):
|
|
|
|
"""Return the Swisscom device scanner."""
|
|
|
|
scanner = SwisscomDeviceScanner(config[DOMAIN])
|
|
|
|
|
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
class SwisscomDeviceScanner(DeviceScanner):
|
2016-11-05 20:04:44 +00:00
|
|
|
"""This class queries a router running Swisscom Internet-Box firmware."""
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize the scanner."""
|
|
|
|
self.host = config[CONF_HOST]
|
|
|
|
self.last_results = {}
|
|
|
|
|
|
|
|
# Test the router is accessible.
|
|
|
|
data = self.get_swisscom_data()
|
|
|
|
self.success_init = data is not None
|
|
|
|
|
|
|
|
def scan_devices(self):
|
|
|
|
"""Scan for new devices and return a list with found device IDs."""
|
|
|
|
self._update_info()
|
2019-07-31 19:25:30 +00:00
|
|
|
return [client["mac"] for client in self.last_results]
|
2016-11-05 20:04:44 +00:00
|
|
|
|
|
|
|
def get_device_name(self, device):
|
|
|
|
"""Return the name of the given device or None if we don't know."""
|
|
|
|
if not self.last_results:
|
|
|
|
return None
|
|
|
|
for client in self.last_results:
|
2019-07-31 19:25:30 +00:00
|
|
|
if client["mac"] == device:
|
|
|
|
return client["host"]
|
2016-11-05 20:04:44 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def _update_info(self):
|
|
|
|
"""Ensure the information from the Swisscom router is up to date.
|
|
|
|
|
|
|
|
Return boolean if scanning successful.
|
|
|
|
"""
|
|
|
|
if not self.success_init:
|
|
|
|
return False
|
|
|
|
|
2017-07-24 14:45:02 +00:00
|
|
|
_LOGGER.info("Loading data from Swisscom Internet Box")
|
|
|
|
data = self.get_swisscom_data()
|
|
|
|
if not data:
|
|
|
|
return False
|
2016-11-05 20:04:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
active_clients = [client for client in data.values() if client["status"]]
|
2017-07-24 14:45:02 +00:00
|
|
|
self.last_results = active_clients
|
|
|
|
return True
|
2016-11-05 20:04:44 +00:00
|
|
|
|
|
|
|
def get_swisscom_data(self):
|
|
|
|
"""Retrieve data from Swisscom and return parsed result."""
|
2019-09-03 19:14:39 +00:00
|
|
|
url = f"http://{self.host}/ws"
|
2019-07-31 19:25:30 +00:00
|
|
|
headers = {CONTENT_TYPE: "application/x-sah-ws-4-call+json"}
|
2016-11-18 22:05:03 +00:00
|
|
|
data = """
|
|
|
|
{"service":"Devices", "method":"get",
|
|
|
|
"parameters":{"expression":"lan and not self"}}"""
|
|
|
|
|
2016-11-05 20:04:44 +00:00
|
|
|
devices = {}
|
Fix unhandled exception when Swisscom Internet Box is not responsive (#28618)
* Update device_tracker.py
From time to time, Swisscom Internet Box fails to respond and this causes an exception, which is currently not handled by the code:
Traceback (most recent call last):
File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/device_tracker/setup.py", line 164, in async_device_tracker_scan
found_devices = await scanner.async_scan_devices()
File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/swisscom/device_tracker.py", line 46, in scan_devices
self._update_info()
File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/swisscom/device_tracker.py", line 67, in _update_info
data = self.get_swisscom_data()
File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/swisscom/device_tracker.py", line 83, in get_swisscom_data
request = requests.post(url, headers=headers, data=data, timeout=10)
File "/srv/homeassistant/lib/python3.7/site-packages/requests/api.py", line 116, in post
return request('post', url, data=data, json=json, **kwargs)
File "/srv/homeassistant/lib/python3.7/site-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/srv/homeassistant/lib/python3.7/site-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/srv/homeassistant/lib/python3.7/site-packages/requests/sessions.py", line 686, in send
r.content
File "/srv/homeassistant/lib/python3.7/site-packages/requests/models.py", line 828, in content
self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
File "/srv/homeassistant/lib/python3.7/site-packages/requests/models.py", line 757, in generate
raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.1.1', port=80): Read timed out.
I've just added a try-except around the post.
* Update device_tracker.py
Addressed blank line issue reported by flake8
* Update device_tracker.py
Fixed alignment to be Black compliant.
* Update device_tracker.py
Fixed one more alignment issue
2019-11-08 19:01:35 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
request = requests.post(url, headers=headers, data=data, timeout=10)
|
|
|
|
except (
|
|
|
|
requests.exceptions.ConnectionError,
|
|
|
|
requests.exceptions.Timeout,
|
|
|
|
requests.exceptions.ConnectTimeout,
|
|
|
|
):
|
|
|
|
_LOGGER.info("No response from Swisscom Internet Box")
|
|
|
|
return devices
|
|
|
|
|
2019-11-15 09:52:15 +00:00
|
|
|
if "status" not in request.json():
|
|
|
|
_LOGGER.info("No status in response from Swisscom Internet Box")
|
|
|
|
return devices
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
for device in request.json()["status"]:
|
2021-03-23 13:36:43 +00:00
|
|
|
with suppress(KeyError, requests.exceptions.RequestException):
|
2019-07-31 19:25:30 +00:00
|
|
|
devices[device["Key"]] = {
|
|
|
|
"ip": device["IPAddress"],
|
|
|
|
"mac": device["PhysAddress"],
|
|
|
|
"host": device["Name"],
|
|
|
|
"status": device["Active"],
|
|
|
|
}
|
2016-11-05 20:04:44 +00:00
|
|
|
return devices
|