1 """Support for Actiontec MI424WR (Verizon FIOS) routers."""
3 from __future__
import annotations
7 from typing
import Final
9 import voluptuous
as vol
12 DOMAIN
as DEVICE_TRACKER_DOMAIN,
13 PLATFORM_SCHEMA
as DEVICE_TRACKER_PLATFORM_SCHEMA,
21 from .const
import LEASES_REGEX
22 from .model
import Device
24 _LOGGER: Final = logging.getLogger(__name__)
26 PLATFORM_SCHEMA: Final = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
28 vol.Required(CONF_HOST): cv.string,
29 vol.Required(CONF_PASSWORD): cv.string,
30 vol.Required(CONF_USERNAME): cv.string,
36 hass: HomeAssistant, config: ConfigType
37 ) -> ActiontecDeviceScanner |
None:
38 """Validate the configuration and return an Actiontec scanner."""
40 return scanner
if scanner.success_init
else None
44 """Class which queries an actiontec router for connected devices."""
46 def __init__(self, config: ConfigType) ->
None:
47 """Initialize the scanner."""
48 self.host: str = config[CONF_HOST]
49 self.username: str = config[CONF_USERNAME]
50 self.password: str = config[CONF_PASSWORD]
56 """Scan for new devices and return a list with found device IDs."""
58 return [client.mac_address
for client
in self.
last_resultslast_results]
61 """Return the name of the given device or None if we don't know."""
63 if client.mac_address == device:
64 return client.ip_address
68 """Ensure the information from the router is up to date.
70 Return boolean if scanning successful.
72 _LOGGER.debug(
"Scanning")
79 device
for device
in actiontec_data
if device.timevalid > -60
81 _LOGGER.debug(
"Scan successful")
85 """Retrieve data from Actiontec MI424WR and return parsed result."""
87 telnet = telnetlib.Telnet(self.host)
88 telnet.read_until(b
"Username: ")
89 telnet.write((f
"{self.username}\n").encode(
"ascii"))
90 telnet.read_until(b
"Password: ")
91 telnet.write((f
"{self.password}\n").encode(
"ascii"))
92 prompt = telnet.read_until(b
"Wireless Broadband Router> ").split(b
"\n")[-1]
93 telnet.write(b
"firewall mac_cache_dump\n")
95 telnet.read_until(prompt)
96 leases_result = telnet.read_until(prompt).split(b
"\n")[1:-1]
97 telnet.write(b
"exit\n")
99 _LOGGER.exception(
"Unexpected response from router")
101 except ConnectionRefusedError:
102 _LOGGER.exception(
"Connection refused by router. Telnet enabled?")
105 devices: list[Device] = []
106 for lease
in leases_result:
107 match = LEASES_REGEX.search(lease.decode(
"utf-8"))
108 if match
is not None:
112 match.group(
"mac").upper(),
113 int(match.group(
"timevalid")),
list[str] scan_devices(self)
str|None get_device_name(self, str device)
None __init__(self, ConfigType config)
list[Device]|None get_actiontec_data(self)
ActiontecDeviceScanner|None get_scanner(HomeAssistant hass, ConfigType config)