2019-04-03 15:40:03 +00:00
|
|
|
"""Support for French FAI Bouygues Bbox routers."""
|
2021-03-17 22:34:25 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-10-14 02:43:51 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
from datetime import timedelta
|
2018-09-23 08:37:53 +00:00
|
|
|
import logging
|
2016-10-30 08:58:34 +00:00
|
|
|
|
2019-10-12 19:51:32 +00:00
|
|
|
import pybbox
|
2018-09-23 08:37:53 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
2018-09-23 08:37:53 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
2022-01-10 22:42:39 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-09-23 08:37:53 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-10 22:42:39 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2016-10-14 02:43:51 +00:00
|
|
|
from homeassistant.util import Throttle
|
2018-09-23 08:37:53 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2016-10-14 02:43:51 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-10-30 08:58:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_HOST = "192.168.1.254"
|
2018-09-23 08:37:53 +00:00
|
|
|
|
2016-10-30 08:58:34 +00:00
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=60)
|
2016-10-14 02:43:51 +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_HOST): cv.string}
|
|
|
|
)
|
2018-09-23 08:37:53 +00:00
|
|
|
|
2016-10-14 02:43:51 +00:00
|
|
|
|
2022-09-07 08:58:54 +00:00
|
|
|
def get_scanner(hass: HomeAssistant, config: ConfigType) -> BboxDeviceScanner | None:
|
2016-10-14 02:43:51 +00:00
|
|
|
"""Validate the configuration and return a Bbox scanner."""
|
|
|
|
scanner = BboxDeviceScanner(config[DOMAIN])
|
|
|
|
|
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
Device = namedtuple("Device", ["mac", "name", "ip", "last_update"])
|
2016-10-14 02:43:51 +00:00
|
|
|
|
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
class BboxDeviceScanner(DeviceScanner):
|
2023-03-03 10:26:13 +00:00
|
|
|
"""Scanner for devices connected to the bbox."""
|
2016-10-14 02:43:51 +00:00
|
|
|
|
|
|
|
def __init__(self, config):
|
2018-09-23 08:37:53 +00:00
|
|
|
"""Get host from config."""
|
2019-02-05 10:12:09 +00:00
|
|
|
|
2018-09-23 08:37:53 +00:00
|
|
|
self.host = config[CONF_HOST]
|
|
|
|
|
2016-10-14 02:43:51 +00:00
|
|
|
"""Initialize the scanner."""
|
2021-03-17 22:34:25 +00:00
|
|
|
self.last_results: list[Device] = []
|
2016-10-14 02:43:51 +00:00
|
|
|
|
|
|
|
self.success_init = self._update_info()
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.info("Scanner initialized")
|
2016-10-14 02:43:51 +00:00
|
|
|
|
|
|
|
def scan_devices(self):
|
|
|
|
"""Scan for new devices and return a list with found device IDs."""
|
|
|
|
self._update_info()
|
|
|
|
|
|
|
|
return [device.mac for device in self.last_results]
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def get_device_name(self, device):
|
2016-10-14 02:43:51 +00:00
|
|
|
"""Return the name of the given device or None if we don't know."""
|
2019-07-31 19:25:30 +00:00
|
|
|
filter_named = [
|
|
|
|
result.name for result in self.last_results if result.mac == device
|
|
|
|
]
|
2016-10-14 02:43:51 +00:00
|
|
|
|
|
|
|
if filter_named:
|
|
|
|
return filter_named[0]
|
2017-07-06 06:30:01 +00:00
|
|
|
return None
|
2016-10-14 02:43:51 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_SCANS)
|
|
|
|
def _update_info(self):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Check the Bbox for devices.
|
2016-10-14 02:43:51 +00:00
|
|
|
|
|
|
|
Returns boolean if scanning successful.
|
|
|
|
"""
|
2021-03-19 14:26:36 +00:00
|
|
|
_LOGGER.info("Scanning")
|
2016-10-14 02:43:51 +00:00
|
|
|
|
2018-09-23 08:37:53 +00:00
|
|
|
box = pybbox.Bbox(ip=self.host)
|
2016-10-14 02:43:51 +00:00
|
|
|
result = box.get_all_connected_devices()
|
|
|
|
|
|
|
|
now = dt_util.now()
|
|
|
|
last_results = []
|
|
|
|
for device in result:
|
2019-07-31 19:25:30 +00:00
|
|
|
if device["active"] != 1:
|
2016-10-14 02:43:51 +00:00
|
|
|
continue
|
|
|
|
last_results.append(
|
2019-07-31 19:25:30 +00:00
|
|
|
Device(
|
|
|
|
device["macaddress"], device["hostname"], device["ipaddress"], now
|
|
|
|
)
|
|
|
|
)
|
2016-10-14 02:43:51 +00:00
|
|
|
|
|
|
|
self.last_results = last_results
|
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.info("Scan successful")
|
2016-10-14 02:43:51 +00:00
|
|
|
return True
|