2019-04-03 15:40:03 +00:00
|
|
|
"""Support for UPC ConnectBox router."""
|
2021-03-18 13:43:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-01-09 16:08:37 +00:00
|
|
|
import logging
|
|
|
|
|
2019-09-11 11:17:07 +00:00
|
|
|
from connect_box import ConnectBox
|
|
|
|
from connect_box.exceptions import ConnectBoxError, ConnectBoxLoginError
|
2017-01-09 16:08:37 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.device_tracker import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
DeviceScanner,
|
|
|
|
)
|
2019-09-11 11:17:07 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP
|
2017-11-04 19:04:05 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-01-09 16:08:37 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_IP = "192.168.0.1"
|
2017-01-09 16:08:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
2019-09-11 11:17:07 +00:00
|
|
|
{
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_HOST, default=DEFAULT_IP): cv.string,
|
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-01-09 16:08:37 +00:00
|
|
|
|
|
|
|
|
2018-10-01 06:59:45 +00:00
|
|
|
async def async_get_scanner(hass, config):
|
2017-01-09 16:08:37 +00:00
|
|
|
"""Return the UPC device scanner."""
|
2019-09-11 11:17:07 +00:00
|
|
|
conf = config[DOMAIN]
|
|
|
|
session = hass.helpers.aiohttp_client.async_get_clientsession()
|
|
|
|
connect_box = ConnectBox(session, conf[CONF_PASSWORD], host=conf[CONF_HOST])
|
|
|
|
|
|
|
|
# Check login data
|
|
|
|
try:
|
|
|
|
await connect_box.async_initialize_token()
|
|
|
|
except ConnectBoxLoginError:
|
|
|
|
_LOGGER.error("ConnectBox login data error!")
|
|
|
|
return None
|
|
|
|
except ConnectBoxError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def _shutdown(event):
|
|
|
|
"""Shutdown event."""
|
|
|
|
await connect_box.async_close_session()
|
2017-01-09 16:08:37 +00:00
|
|
|
|
2019-09-11 11:17:07 +00:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown)
|
|
|
|
|
|
|
|
return UPCDeviceScanner(connect_box)
|
2017-01-09 16:08:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UPCDeviceScanner(DeviceScanner):
|
|
|
|
"""This class queries a router running UPC ConnectBox firmware."""
|
|
|
|
|
2021-05-20 15:00:19 +00:00
|
|
|
def __init__(self, connect_box: ConnectBox) -> None:
|
2017-01-09 16:08:37 +00:00
|
|
|
"""Initialize the scanner."""
|
2019-09-11 11:17:07 +00:00
|
|
|
self.connect_box: ConnectBox = connect_box
|
2017-01-09 16:08:37 +00:00
|
|
|
|
2021-03-18 13:43:52 +00:00
|
|
|
async def async_scan_devices(self) -> list[str]:
|
2017-01-09 16:08:37 +00:00
|
|
|
"""Scan for new devices and return a list with found device IDs."""
|
2017-01-17 22:35:02 +00:00
|
|
|
try:
|
2019-09-11 11:17:07 +00:00
|
|
|
await self.connect_box.async_get_devices()
|
|
|
|
except ConnectBoxError:
|
2017-01-25 04:59:34 +00:00
|
|
|
return []
|
2017-01-09 16:08:37 +00:00
|
|
|
|
2019-09-11 11:17:07 +00:00
|
|
|
return [device.mac for device in self.connect_box.devices]
|
2017-03-30 07:50:53 +00:00
|
|
|
|
2021-03-18 13:43:52 +00:00
|
|
|
async def async_get_device_name(self, device: str) -> str | None:
|
2019-09-11 11:17:07 +00:00
|
|
|
"""Get the device name (the name of the wireless device not used)."""
|
|
|
|
for connected_device in self.connect_box.devices:
|
2020-09-02 12:56:32 +00:00
|
|
|
if (
|
|
|
|
connected_device.mac == device
|
|
|
|
and connected_device.hostname.lower() != "unknown"
|
|
|
|
):
|
|
|
|
return connected_device.hostname
|
2017-01-09 16:08:37 +00:00
|
|
|
|
2019-09-11 11:17:07 +00:00
|
|
|
return None
|