[Custom component] AsusRouter integration

GitHub Release Installations

:books: Documentation

You can find the documentation here:

:book: Additional useful (or not writing about Asus routers)

:loudspeaker: Do you want to add AsusRouter to HA Core integrations?

Unfortunately, the idea was rejected. But our feature request is one of the largest on the Community forum. We can still get into top-10 of all time :wink:

Add AsusRouter integration to HA Core - Feature Requests - Home Assistant Community (home-assistant.io)

:hatching_chick: Intro (from 2022-04-22)

Hello,

I would like to share a small project Iā€™ve done to monitor my Asus router from Home Assistant. It is now available as a default HACS repository:

HACS -> Integrations -> Explore & Download Repositories -> AsusRouter

There could be a question of why am I doing this if there is an AsusWRT component in HA - because that one doesnā€™t really work well for me and shows fake traffic usage (if compare to web or phone apps of the router itself). Also, this integration uses Asus native HTTP API instead of SSH or telnet, which is the only option to implement control functions in the future.

:rocket: Device and Firmware compatibility

The integration does support virtually any Asus router. The latest list of devices and tested FW can be found here:

AsusRouter supports all the FW versions starting with 3.0.0.4.380.70. Older devices might be missing some of the features, but they also work well.

:thermometer: Sensors and controls

AsusRouter supports the following platforms:

  • binary_sensor
  • button
  • device_tracker
  • light
  • sensor
  • switch
  • update

The full list of features can be found here:

But the shortlist includes:

  • Connected devices monitoring
  • AiMesh monitoring
  • CPU, load average (Merlin only), RAM, WAN temperature and ethernet ports monitoring
  • Network interfaces monitoring (speeds, traffic)
  • WLAN, guest WLAN monitoring and control
  • OpenVPN monitoring and control
  • Parental control
  • LED control (FW: 386.0+)

:hammer_and_pick: with more features coming soon.


:star: Star AsusRouter repository


:coffee: Support AsusRouter

This integration is a free-time project. If you like it, you can support me by buying a coffee.

Buy Me A Coffee

10 Likes

I will give it a go but in daily life I am not too interested in my router(s) so please donot expect any quick results (unless I find bugs)

And ā€¦the bug :slight_smile:
I removed the asuswrt integration, added yours HACS, rebooted and tried to add in integrationsā€¦this is the result
image

Could you please check your log for errors? It should say, what went wrong with config flow

of courseā€¦stupid me

2022-04-27 12:43:15 ERROR (SyncWorker_8) [homeassistant.util.package] Unable to install package asusrouter==0.2.7: ERROR: Could not find a version that satisfies the requirement asusrouter==0.2.7 (from versions: none)

ERROR: No matching distribution found for asusrouter==0.2.7

it also mentions pip version to be upgraded to 22.0.4. ( I am on 3) but cannot imagine this is related or?

Sorry, this is my mistake. HA works from Python 3.9 and AsusRouter library was with 3.10+ support only. Iā€™ve added support for 3.9 and updated this integration to 0.1.2 for that as well. Should be working now, you just need to update to 0.1.2 from HACS

Next ā€¦ note that I am doing this without much focus inbetween other stuff
I am on 2022.4.3 container version

image

2022-04-27 14:03:33 ERROR (MainThread) [homeassistant.loader] Unexpected exception importing platform custom_components.asusrouter.config_flow
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/loader.py", line 603, in get_platform
cache[full_name] = self._import_platform(platform_name)
File "/usr/src/homeassistant/homeassistant/loader.py", line 620, in _import_platform
return importlib.import_module(f"{self.pkg_path}.{platform_name}")
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/config/custom_components/asusrouter/__init__.py", line 8, in <module>
from .router import AsusRouterObj
File "/config/custom_components/asusrouter/router.py", line 12, in <module>
from asusrouter import AsusRouter
File "/usr/local/lib/python3.9/site-packages/asusrouter/__init__.py", line 15, in <module>
from . import(
File "/usr/local/lib/python3.9/site-packages/asusrouter/helpers.py", line 250, in <module>
async def async_transform_port_speed(value : str | None = None) -> int | None:
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
2022-04-27 14:03:33 ERROR (MainThread) [homeassistant.config_entries] Error occurred loading configuration flow for integration asusrouter: Exception importing custom_components.asusrouter.config_flow

Could you please check with the 0.1.3 update from HACS now?

we are getting there

I however only see limited entities, not sure what to expect

And this is something I would not want to haveā€¦the integration is adding other stuff but not as part of device tracking

There are supposed to be WAN Download / Upload / Down speed / Up speed sensors enabled by default. A bit strange, that it does not report the most important part.
Do you mind trying the following?

  • Add to your HA configuration.yaml
logger:
  logs:
    custom_components.asusrouter: debug
  • Restart HA
  • Check the log when HA started

The integration should report in debugging all the available interfaces to measure traffic just when loaded. Looks like this:

[custom_components.asusrouter.bridge] Available network stat sensors: ['WAN_rx', 'WAN_tx', 'WAN_rx_speed', 'WAN_tx_speed', 'WIRED_rx', 'WIRED_tx', 'WIRED_rx_speed', 'WIRED_tx_speed', 'BRIDGE_rx', 'BRIDGE_tx', 'BRIDGE_rx_speed', 'BRIDGE_tx_speed', 'WLAN0_rx', 'WLAN0_tx', 'WLAN0_rx_speed', 'WLAN0_tx_speed', 'WLAN1_rx', 'WLAN1_tx', 'WLAN1_rx_speed', 'WLAN1_tx_speed', 'USB_rx', 'USB_tx', 'USB_rx_speed', 'USB_tx_speed']

It would help to understand the problem.

Also, if you will be checking debug, there should be something similar to [custom_components.asusrouter.bridge] Available CPU sensors: ['total', 'core_1'] nearby. I suppose, for your AC86U, it will show core_2 as well?


Regarding the devices, they are not created by the integration. It creates only device_tracker.device_name entities. But marks their MAC in the HA registry. So if you already had a device with the same MAC address via another integration - device_trarcker will be added to it. Thatā€™s why it says e.g. ESPHome, ASUS Router in the Integration field of the device.

Or did it change device names or any other info?

OKā€¦this will have to be for tomorrow then. I was actually testing this on my main instance but with this I will quickly setup a dev instanceā€¦

hereby the lo
on my dev env it does not map to the other 3 devices possibly because I did not set them up as integrations. Still, same entities as above
Log

2022-04-28 08:16:33 DEBUG (MainThread) [custom_components.asusrouter.router] Checking devices for ASUS router 192.168.1.44
2022-04-28 08:16:33 DEBUG (MainThread) [custom_components.asusrouter.bridge] Available CPU sensors: ['total', 'core_1', 'core_2']
2022-04-28 08:16:33 DEBUG (MainThread) [custom_components.asusrouter.bridge] Available network stat sensors: ['WIRED_rx', 'WIRED_tx', 'WIRED_rx_speed', 'WIRED_tx_speed', 'BRIDGE_rx', 'BRIDGE_tx', 'BRIDGE_rx_speed', 'BRIDGE_tx_speed', 'WLAN0_rx', 'WLAN0_tx', 'WLAN0_rx_speed', 'WLAN0_tx_speed', 'WLAN1_rx', 'WLAN1_tx', 'WLAN1_rx_speed', 'WLAN1_tx_speed', 'USB_rx', 'USB_tx', 'USB_rx_speed', 'USB_tx_speed']
2022-04-28 08:16:33 DEBUG (MainThread) [custom_components.asusrouter.router] Finished fetching cpu data in 0.000 seconds (success: True)
2022-04-28 08:16:33 DEBUG (MainThread) [custom_components.asusrouter.router] Finished fetching ram data in 0.000 seconds (success: True)
2022-04-28 08:16:33 DEBUG (MainThread) [custom_components.asusrouter.router] Finished fetching network_stat data in 0.000 seconds (success: True)
2022-04-28 08:16:34 DEBUG (MainThread) [custom_components.asusrouter.router] Finished fetching misc data in 0.071 seconds (success: True)
2022-04-28 08:16:34 DEBUG (MainThread) [custom_components.asusrouter.router] Finished fetching devices data in 0.000 seconds (success: True)
2022-04-28 08:16:34 DEBUG (MainThread) [custom_components.asusrouter.router] Finished fetching cpu data in 0.000 seconds (success: True)
2022-04-28 08:16:34 DEBUG (MainThread) [custom_components.asusrouter.router] Finished fetching ram data in 0.000 seconds (success: True)
2022-04-28 08:16:34 DEBUG (MainThread) [custom_components.asusrouter.router] Finished fetching network_stat data in 0.000 seconds (success: True)
2022-04-28 08:16:34 DEBUG (MainThread) [custom_components.asusrouter.router] Finished fetching devices data in 0.000 seconds (success: True)
2022-04-28 08:16:34 DEBUG (MainThread) [custom_components.asusrouter.router] Finished fetching misc data in 0.086 seconds (success: True)
2022-04-28 08:17:04 DEBUG (MainThread) [custom_components.asusrouter.router] Checking devices for ASUS router 192.168.1.44
2022-04-28 08:17:34 DEBUG (MainThread) [custom_components.asusrouter.router] Checking devices for ASUS router 192.168.1.44
2022-04-28 08:18:04 DEBUG (MainThread) [custom_components.asusrouter.router] Checking devices for ASUS router 192.168.1.44

Looks like your router model reports the main traffic in another way than the one I have (and hoped to be universal enough among Asus routers)

If you are still up to trying more, here is a small python snippet, which would tell, what exactly the device reports. You would need to run it from the environment with the asusrouter library being installed (e.g. from Home Assistant venv, since there it was already installed by the integration)

"""Small test for available data from your AsusRouter"""

from asusrouter import AsusRouter
import asyncio

MONITOR_MAIN = {
    "cpu_usage" : "appobj",
    "memory_usage" : "appobj",
    "netdev" : "appobj"
}


async def gather_data(host, username, password, use_ssl):
    """Method to gether all the known data"""

    # Initialize
    router = AsusRouter(host = host, username = username, password = password, use_ssl = use_ssl)

    # Connect
    await router.connection.async_connect()

    # Get monitor_main data in raw
    data = await router.async_hook(await router.async_compile_hook(MONITOR_MAIN))
    print(data)

    # Disconnect
    await router.connection.async_disconnect()


loop = asyncio.get_event_loop()

# Please, put your values in here
_host = "IP_or_hostname_here"
_username = "admin"
_password = "password"
_use_ssl = True

loop.run_until_complete(gather_data(_host, _username, _password, _use_ssl))

loop.close()

Just put the code to some local script.py (without forgetting to put in your values for _host and _password in it). Then run it with python3 script.py. It will print out raw data for network interfaces traffic, as well as CPU and RAM.

Would look like this:

{'cpu_usage': {'cpu1_total': '2507529', 'cpu1_usage': '503093'}, 'memory_usage': {'mem_total': '262144', 'mem_free': '170660', 'mem_used': '91484'}, 'netdev': {'INTERNET_rx': '0xaaa3b94b', 'INTERNET_tx': '0x1198f53c', 'BRIDGE_rx': '0x2012997', 'BRIDGE_tx': '0x5a525cc', 'WIRED_rx': '0x3422e085', 'WIRED_tx': '0x3133c49c', 'WIRELESS0_rx': '0xe80fce8', 'WIRELESS0_tx': '0xa7ab6ed', 'WIRELESS1_rx': '0x8e3205c', 'WIRELESS1_tx': '0xad52d52f'}}

The ones used for WAN data in the integration are INTERNET_rx and INTERNET_tx. Probably, in your case, it is called differently. Just need to find out how - then I can try to implement it.

installed the latest version and ran script

{'cpu_usage': {'cpu1_total': '124489334', 'cpu1_usage': '1210329', 'cpu2_total': '124557209', 'cpu2_usage': '761584'}, 'memory_usage': {'mem_total': '524288', 'mem_free': '234912', 'mem_used': '289376'}, 'netdev': {'BRIDGE_rx': '0x766d56fc', 'BRIDGE_tx': '0x190fb63b', 'WIRED_rx': '0x366dcd3910', 'WIRED_tx': '0x1092059a01', 'WIRELESS0_rx': '0x26e87403', 'WIRELESS0_tx': '0x1dd488180', 'WIRELESS1_rx': '0x1eedcabbba', 'WIRELESS1_tx': '0x5e30046ec9'}}

So here is where the issue lies. Apparently, AC86U doesnā€™t report INTERNET values. Iā€™ve implemented a fix for it, so BRIDGE values would be used in such a case.

You can try now with integration version 0.1.5 (available in HACS already). I hope, it will work from the first attempt :slightly_smiling_face:

okā€¦it sort of works but the up/dowload speed values seem to be factor 10 lower then what the other integration showsā€¦I donot know which one is correct though.
And what do the up/dowload sensors register, do they accumulate inside HA as these are totally different ā€¦ I am comparing standard on prod machine vs yours on dev envā€¦hence that might explain the difference

Another oddity is the connected devicesā€¦The router shows 3 via wifi, normal integration shows 2 and yours the total of identified devices (tracker)ā€¦which is also shown on the routerā€¦so here the ā€œnormalā€ integration is odd

The question is whether they are correct in corresponding to the router own data or not. AsusRouter integration should show values the same way as a web panel or mobile app from Asus. Of course, your one is not exactly the same and provides a bit different info (why it didnā€™t work before).

  • In the case of AsusRouter, Download and Upload sensors are taken directly from the device using its HTTP API, the same way as the web panel/app.
  • AsusWRT library also is getting values from the device, but performing cat /sys/class/net/{}/statistics/rx_bytes command on the selected interface (default is eth0) when connected via ssh/telnet.

In both cases:

  • They will reset to 0 when the router is restarted or if reaches 4GB (firmware limitation, at least on my one).
  • Speeds are just calculated from 2 measurements of traffic, separated in time.

What I can say - AsusWRT is giving me ridiculously high values for Upload and quite a bit higher Downloads. I have a modem connected as WAN of the router. Neither of that high traffic gets to it. That was one of the reasons to try with my own python library and HA integration. At least, it definitely reports not what I would expect to get from it (neither WAN - the most important data, nor LAN or WLAN traffics, but some combination of them, even though not just a sum of all mentioned). So both internal traffic and external one contribute to it, which makes it not too useful.

Considering devices, it again should work like the web panel/app.

My comparison of data. Core AsusWRT integration on the right, my AsusRouter in the middle and some router info on the left.

  • For AsusWRT, Upload never gets to 0 and is constantly high (at least 0.8 Mbit/s)

  • AsusWRT shows 1 more device than the router knows of at the moment

  • Speed for AsusRouter is proportional to what the router knows on WAN

So in general, if the data for traffic and speed is like the web panel reports for WAN, this is something real. Just be careful, the web panel shows speeds in KB/s or other byte values, not bits/s, as shown in the integration, so a factor of 8 is needed.

AsusRouter is now available as a default HACS repository :smiley:

No need anymore to add it manually. Just:
HACS -> Integrations -> Explore & Download Repositories -> AsusRouter