1 """Support for THOMSON routers."""
3 from __future__
import annotations
9 import voluptuous
as vol
12 DOMAIN
as DEVICE_TRACKER_DOMAIN,
13 PLATFORM_SCHEMA
as DEVICE_TRACKER_PLATFORM_SCHEMA,
21 _LOGGER = logging.getLogger(__name__)
23 _DEVICES_REGEX = re.compile(
24 r"(?P<mac>(([0-9a-f]{2}[:-]){5}([0-9a-f]{2})))\s"
25 r"(?P<ip>([0-9]{1,3}[\.]){3}[0-9]{1,3})\s+"
26 r"(?P<status>([^\s]+))\s+"
27 r"(?P<type>([^\s]+))\s+"
28 r"(?P<intf>([^\s]+))\s+"
29 r"(?P<hwintf>([^\s]+))\s+"
33 PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
35 vol.Required(CONF_HOST): cv.string,
36 vol.Required(CONF_PASSWORD): cv.string,
37 vol.Required(CONF_USERNAME): cv.string,
42 def get_scanner(hass: HomeAssistant, config: ConfigType) -> ThomsonDeviceScanner |
None:
43 """Validate the configuration and return a THOMSON scanner."""
46 return scanner
if scanner.success_init
else None
50 """Class which queries a router running THOMSON firmware."""
53 """Initialize the scanner."""
54 self.
hosthost = config[CONF_HOST]
64 """Scan for new devices and return a list with found device IDs."""
66 return [client[
"mac"]
for client
in self.
last_resultslast_results]
69 """Return the name of the given device or None if we don't know."""
73 if client[
"mac"] == device:
78 """Ensure the information from the THOMSON router is up to date.
80 Return boolean if scanning successful.
85 _LOGGER.debug(
"Checking ARP")
91 client
for client
in data.values()
if client[
"status"].find(
"C") != -1
97 """Retrieve data from THOMSON and return parsed result."""
99 telnet = telnetlib.Telnet(self.
hosthost)
100 telnet.read_until(b
"Username : ")
101 telnet.write((self.
usernameusername +
"\r\n").encode(
"ascii"))
102 telnet.read_until(b
"Password : ")
103 telnet.write((self.
passwordpassword +
"\r\n").encode(
"ascii"))
104 telnet.read_until(b
"=>")
105 telnet.write(b
"hostmgr list\r\n")
106 devices_result = telnet.read_until(b
"=>").split(b
"\r\n")
107 telnet.write(b
"exit\r\n")
109 _LOGGER.exception(
"Unexpected response from router")
111 except ConnectionRefusedError:
112 _LOGGER.exception(
"Connection refused by router. Telnet enabled?")
116 for device
in devices_result:
117 if match := _DEVICES_REGEX.search(device.decode(
"utf-8")):
118 devices[match.group(
"ip")] = {
119 "ip": match.group(
"ip"),
120 "mac": match.group(
"mac").upper(),
121 "host": match.group(
"host"),
122 "status": match.group(
"status"),
def get_device_name(self, device)
def __init__(self, config)
def get_thomson_data(self)
ThomsonDeviceScanner|None get_scanner(HomeAssistant hass, ConfigType config)