2019-04-03 15:40:03 +00:00
|
|
|
"""Support for FRITZ!Box routers."""
|
2015-12-09 19:22:40 +00:00
|
|
|
import logging
|
|
|
|
|
2019-10-18 00:17:24 +00:00
|
|
|
from fritzconnection import FritzHosts # pylint: disable=import-error
|
2016-09-02 04:28:03 +00:00
|
|
|
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,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
DeviceScanner,
|
|
|
|
)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
2019-10-18 21:54:56 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-12-09 19:22:40 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DEFAULT_IP = "169.254.1.1" # This IP is valid for all FRITZ!Box routers.
|
2016-09-02 04:28:03 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_HOST, default=CONF_DEFAULT_IP): cv.string,
|
|
|
|
vol.Optional(CONF_PASSWORD, default="admin"): cv.string,
|
|
|
|
vol.Optional(CONF_USERNAME, default=""): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-09-02 04:28:03 +00:00
|
|
|
|
2015-12-09 19:22:40 +00:00
|
|
|
|
|
|
|
def get_scanner(hass, config):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Validate the configuration and return FritzBoxScanner."""
|
2015-12-09 19:22:40 +00:00
|
|
|
scanner = FritzBoxScanner(config[DOMAIN])
|
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
class FritzBoxScanner(DeviceScanner):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""This class queries a FRITZ!Box router."""
|
2015-12-09 19:22:40 +00:00
|
|
|
|
|
|
|
def __init__(self, config):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Initialize the scanner."""
|
2015-12-09 19:22:40 +00:00
|
|
|
self.last_results = []
|
2016-09-02 04:28:03 +00:00
|
|
|
self.host = config[CONF_HOST]
|
|
|
|
self.username = config[CONF_USERNAME]
|
|
|
|
self.password = config[CONF_PASSWORD]
|
2015-12-09 19:22:40 +00:00
|
|
|
self.success_init = True
|
|
|
|
|
2016-03-07 17:12:06 +00:00
|
|
|
# Establish a connection to the FRITZ!Box.
|
2015-12-09 19:22:40 +00:00
|
|
|
try:
|
2019-10-18 00:17:24 +00:00
|
|
|
self.fritz_box = FritzHosts(
|
2019-07-31 19:25:30 +00:00
|
|
|
address=self.host, user=self.username, password=self.password
|
|
|
|
)
|
2015-12-09 19:31:40 +00:00
|
|
|
except (ValueError, TypeError):
|
2015-12-09 19:22:40 +00:00
|
|
|
self.fritz_box = None
|
|
|
|
|
|
|
|
# At this point it is difficult to tell if a connection is established.
|
2016-03-07 17:12:06 +00:00
|
|
|
# So just check for null objects.
|
2015-12-09 19:22:40 +00:00
|
|
|
if self.fritz_box is None or not self.fritz_box.modelname:
|
|
|
|
self.success_init = False
|
|
|
|
|
|
|
|
if self.success_init:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.info("Successfully connected to %s", self.fritz_box.modelname)
|
2015-12-09 19:22:40 +00:00
|
|
|
self._update_info()
|
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Failed to establish connection to FRITZ!Box " "with IP: %s", self.host
|
|
|
|
)
|
2015-12-09 19:22:40 +00:00
|
|
|
|
|
|
|
def scan_devices(self):
|
2016-03-07 17:12:06 +00:00
|
|
|
"""Scan for new devices and return a list of found device ids."""
|
2015-12-09 19:22:40 +00:00
|
|
|
self._update_info()
|
|
|
|
active_hosts = []
|
|
|
|
for known_host in self.last_results:
|
2019-07-31 19:25:30 +00:00
|
|
|
if known_host["status"] == "1" and known_host.get("mac"):
|
|
|
|
active_hosts.append(known_host["mac"])
|
2015-12-09 19:22:40 +00:00
|
|
|
return active_hosts
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def get_device_name(self, device):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Return the name of the given device or None if is not known."""
|
2019-07-31 19:25:30 +00:00
|
|
|
ret = self.fritz_box.get_specific_host_entry(device).get("NewHostName")
|
2015-12-09 19:22:40 +00:00
|
|
|
if ret == {}:
|
|
|
|
return None
|
|
|
|
return ret
|
|
|
|
|
2020-01-01 21:03:38 +00:00
|
|
|
def get_extra_attributes(self, device):
|
|
|
|
"""Return the attributes (ip, mac) of the given device or None if is not known."""
|
|
|
|
ip_device = self.fritz_box.get_specific_host_entry(device).get("NewIPAddress")
|
|
|
|
|
|
|
|
if not ip_device:
|
|
|
|
return None
|
|
|
|
return {"ip": ip_device, "mac": device}
|
|
|
|
|
2015-12-09 19:22:40 +00:00
|
|
|
def _update_info(self):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Retrieve latest information from the FRITZ!Box."""
|
2015-12-09 19:22:40 +00:00
|
|
|
if not self.success_init:
|
|
|
|
return False
|
|
|
|
|
2019-08-18 15:08:26 +00:00
|
|
|
_LOGGER.debug("Scanning")
|
2015-12-09 19:22:40 +00:00
|
|
|
self.last_results = self.fritz_box.get_hosts_info()
|
|
|
|
return True
|