Hello,
does anyone have a script template to add to the configuration.yaml in Home Assistant to be able to control the Wifi networks (different ssids, on/off)? I tried and failed so thought maybe someone in the community has already come up with something. Obviously i mean shell commands via ssh to control the wifi.
thanks
There has been a thread recently about Home Assistant. And it seems there is a HACS integration available - have not tested it though…
this is the integration they mentioned:
Ubus unfortunately (and completely unexplainably) breaks my wireguard server (after days of troubleshooting I decided to abandon ubus and also I don't need all that 'stuff'). I really just need to turn my wifi on/off). The two official integrations, and even the ubus integrations on github, don't permit control of wifi. Shell commands via ssh seems to be the way to go.
ubus is omnipresent.
I see - what about this HACS?
https://github.com/izipuho/OpenWRT_control - this “promises” some SSH control for OpenWrt (“It also lets you set device-specific configurations through selectable options.”) - perhaps also Wifi-on/off…
or more generic SSH-integrations:
https://github.com/custom-components/sensor.ssh - this should allow to submit commands over SSH and read back.
https://github.com/zhbjsh/homeassistant-ssh - similar, looks more feature rich…
I should mention
it is a security risk to allow Home Assistant to have SSH access to a remote server - especially if you allow root-ssh into OpenWrt…
As a side note to make it more “security safe” (beside the ubus method - but also this would allow a quite large range of actions to be taken by an intruder…) you could install a mqtt client, if you have mqtt already running on HA?:
opkg update
opkg install mosquitto-client-ssl
then install a script on startup with something like:
#!/bin/sh
mosquitto_sub -h <BROKER_IP> -t "openwrt/wifi/set" -u "user" -P "password" | while read -r msg; do
if [ "$msg" = "restart" ]; then
wifi down
sleep 2
wifi up
fi
done
and then have Homeassistant send MQTT messages to control OpenWrt…
But I have to confess this also seems quite convoluted - it depends, what you already have running on HA…
agreed. all these ( cobbled‑together) options are unnecessarily convoluted (and essentially unsafe). I can't believe in all those years no one capable (I am not) has come up with something more solid.
Yeah, at least MQTT would be “safe” (as in: only hackable if there is a security hole in MQTT itself) as it has no direct access to OpenWrt - it just listens for commands.
In my case that would even be the preferred solution, as I have an MQTT server running on HA anyways (one-click-install in the new Apps/former Add-ons section of HA), used for other integrations I use…
As mqtt goes there is a one maybe two problems.
First I don’t want to create manually every entity for ha. If mqtt is used auto entities should be created.
Second using mqtt over ipv4. There is no actual need to do so if you are using docker containers. Mqtt can only operate inside docker own network and talk to others conataiers inside internal docker network. So no port is exposed on local host and this make it difficult to hack or even listen to the messages. if mqtt is put on ipv4 well then you should probably considered using ssl for it over local wifi.
This integration is good, it could be better. But it offers a lot of data, way more then you need. The problem I noticed with it is that for some reason rpcd daemon start to consume a lot of memory in 24h. Restarting rcpd daemon it release memory back to the system. Probably a bug in rpcd or something is off in integration usage description.
I also use MQTT on HA but going down this road sets off some 'major headache, none stop troubleshooting' alarm bells
I understand. But not sure I understand the “headache” scenario - the point is: All other methods are “complicated” as well, SSH can fail, is unsafe. Ubus does not seem to work for you and is also not the safest.
MQTT is well integrated on HA side (even has multiple integration methods for all kind of use-cases: https://www.home-assistant.io/integrations/?search=mqtt) and on the OpenWrt router side it is really a small script, which listens to MQTT messages - in your case just one or two: Either Wifi toggle or Wifi off/on.
But of course, I agree: it is not a pre-made/easiest method, you have to do it yourself.