1 """Represent the Netgear router and its devices."""
3 from __future__
import annotations
6 from datetime
import timedelta
10 from pynetgear
import Netgear
26 DEFAULT_CONSIDER_HOME,
32 from .errors
import CannotLoginException
34 _LOGGER = logging.getLogger(__name__)
39 host: str |
None =
None,
40 username: str |
None =
None,
41 port: int |
None =
None,
44 """Get the Netgear API and login to it."""
45 api: Netgear = Netgear(password, host, username, port, ssl)
47 if not api.login_try_port():
48 raise CannotLoginException
54 """Representation of a Netgear router."""
56 def __init__(self, hass: HomeAssistant, entry: ConfigEntry) ->
None:
57 """Initialize a Netgear router."""
58 assert entry.unique_id
63 self._host: str = entry.data[CONF_HOST]
64 self._port: int = entry.data[CONF_PORT]
65 self._ssl: bool = entry.data[CONF_SSL]
66 self.
_username_username = entry.data.get(CONF_USERNAME)
71 self.
modemode = MODE_ROUTER
79 consider_home_int = entry.options.get(
80 CONF_CONSIDER_HOME, DEFAULT_CONSIDER_HOME.total_seconds()
84 self.
apiapi: Netgear =
None
87 self.devices: dict[str, Any] = {}
90 """Set up a Netgear router sync portion."""
100 if self.
_info_info
is None:
108 self.
modemode = self.
_info_info.
get(
"DeviceMode", MODE_ROUTER)
112 for entry
in self.
hasshass.config_entries.async_entries(DOMAIN)
113 if entry.disabled_by
is None
115 self.
track_devicestrack_devices = self.
modemode == MODE_ROUTER
or len(enabled_entries) == 1
117 "Netgear track_devices = '%s', device mode '%s'",
122 for model
in MODELS_V2:
123 if self.
modelmodel.startswith(model):
127 if not self.
apiapi.get_attached_devices_2():
130 "Netgear Model '%s' in MODELS_V2 list, but failed to get"
131 " attached devices using V2"
140 """Set up a Netgear router."""
142 if not await self.
hasshass.async_add_executor_job(self.
_setup_setup):
147 device_registry = dr.async_get(self.
hasshass)
148 devices = dr.async_entries_for_config_entry(device_registry, self.
entry_identry_id)
149 for device_entry
in devices:
150 if device_entry.via_device_id
is None:
153 device_mac =
dict(device_entry.connections)[dr.CONNECTION_NETWORK_MAC]
154 self.devices[device_mac] = {
156 "name": device_entry.name,
158 "last_seen": dt_util.utcnow() -
timedelta(days=365),
159 "device_model":
None,
167 "allow_or_block":
None,
173 """Get the devices connected to the router."""
176 return await self.
hasshass.async_add_executor_job(
177 self.
apiapi.get_attached_devices
181 return await self.
hasshass.async_add_executor_job(
182 self.
apiapi.get_attached_devices_2
186 """Update Netgear devices."""
189 now = dt_util.utcnow()
191 if ntg_devices
is None:
194 if _LOGGER.isEnabledFor(logging.DEBUG):
195 _LOGGER.debug(
"Netgear scan result: \n%s", ntg_devices)
197 for ntg_device
in ntg_devices:
198 if ntg_device.mac
is None:
201 device_mac = dr.format_mac(ntg_device.mac)
203 if not self.devices.
get(device_mac):
207 self.devices[device_mac] = ntg_device._asdict()
208 self.devices[device_mac][
"mac"] = device_mac
209 self.devices[device_mac][
"last_seen"] = now
211 for device
in self.devices.values():
212 device[
"active"] = now - device[
"last_seen"] <= self.
_consider_home_consider_home
215 _LOGGER.debug(
"Netgear tracker: new device found")
220 """Get the traffic meter data of the router."""
222 return await self.
hasshass.async_add_executor_job(self.
apiapi.get_traffic_meter)
225 """Perform a speed test and get the results from the router."""
227 return await self.
hasshass.async_add_executor_job(
228 self.
apiapi.get_new_speed_test_result
232 """Check the ethernet link status of the router."""
234 return await self.
hasshass.async_add_executor_job(self.
apiapi.check_ethernet_link)
237 """Allow or block a device connected to the router."""
239 await self.
hasshass.async_add_executor_job(
240 self.
apiapi.allow_block_device, mac, allow_block
244 """Get the system information about utilization of the router."""
246 return await self.
hasshass.async_add_executor_job(self.
apiapi.get_system_info)
249 """Reboot the router."""
251 await self.
hasshass.async_add_executor_job(self.
apiapi.reboot)
254 """Check for new firmware of the router."""
256 return await self.
hasshass.async_add_executor_job(self.
apiapi.check_new_firmware)
259 """Update the router to the latest firmware."""
261 await self.
hasshass.async_add_executor_job(self.
apiapi.update_new_firmware)
265 """Port used by the API."""
266 return self.
apiapi.port
270 """SSL used by the API."""
271 return self.
apiapi.ssl
None async_update_new_firmware(self)
dict[str, Any]|None async_get_speed_test(self)
None async_allow_block_device(self, str mac, str allow_block)
dict[str, Any]|None async_get_link_status(self)
None __init__(self, HomeAssistant hass, ConfigEntry entry)
dict[str, Any]|None async_get_traffic_meter(self)
bool async_update_device_trackers(self, now=None)
dict[str, Any]|None async_check_new_firmware(self)
dict[str, Any]|None async_get_utilization(self)
list async_get_attached_devices(self)
web.Response get(self, web.Request request, str config_key)
dict[str, Any]|None get_info(HomeAssistant hass)
Netgear get_api(str password, str|None host=None, str|None username=None, int|None port=None, bool ssl=False)