Skip to content

Commit

Permalink
Added hotspot to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
max-pfeiffer committed Jun 13, 2024
1 parent c2aa29f commit 1e6649b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ Commands:
run Run commands.
uninstall Uninstall commands.
```
For instance, you can install and run a Wi-Fi hotspot with it:
```shell
$ sudo irrigation-pi install wifi-hotspot --password
```

## Why another irrigation controller software for the Raspberry Pi?
First of all I was searching GitHub and the web for ready to use solutions. And I found a couple.
Expand Down
2 changes: 2 additions & 0 deletions irrigation_pi/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@

HOST: str = "raspberrypi.local"
PORT: str = "80"

WIFI_HOTSPOT_CONNECTION_NAME: str = "Irrigation-Pi"
4 changes: 4 additions & 0 deletions irrigation_pi/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
install_debian_packages,
install_nginx_configuration,
install_systemd_configuration,
install_wifi_hotspot,
)
from irrigation_pi.restart import restart_nginx, restart_uvicorn
from irrigation_pi.run import backend, frontend
Expand All @@ -25,6 +26,7 @@
uninstall_database,
uninstall_nginx_configuration,
uninstall_systemd_configuration,
uninstall_wifi_hotspot,
)


Expand Down Expand Up @@ -54,6 +56,7 @@ def install():
install.add_command(install_database)
install.add_command(install_systemd_configuration)
install.add_command(install_nginx_configuration)
install.add_command(install_wifi_hotspot)


@click.group()
Expand All @@ -71,6 +74,7 @@ def uninstall():
uninstall.add_command(uninstall_database)
uninstall.add_command(uninstall_nginx_configuration)
uninstall.add_command(uninstall_systemd_configuration)
uninstall.add_command(uninstall_wifi_hotspot)


@click.group()
Expand Down
53 changes: 41 additions & 12 deletions irrigation_pi/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
PORT,
SYSTEMD_CONFIG_PATH,
VIRTUAL_ENVIRONMENT_PATH,
WIFI_HOTSPOT_CONNECTION_NAME,
)
from irrigation_pi.utils import (
activate_virtual_environment,
Expand Down Expand Up @@ -142,28 +143,56 @@ def install_nginx_configuration():


@click.command(name="wifi-hotspot")
def install_wifi_hotspot():
"""Install Wi-Fi hotspot.
@click.option("--ssid", default="Irrigation-Pi", help="SSID of Wi-Fi Hotspot.")
@click.password_option(
"--password",
required=True,
help="Password for Wi-Fi Hotspot, minimum length 8 characters.",
)
@click.option(
"--autoconnect",
default="on",
type=click.Choice(["on", "off"], case_sensitive=False),
help="Wi-Fi Hotspot autoconnect.",
)
@click.option(
"--autoconnect-priority", default="100", help="Wi-Fi Hotspot autoconnect-priority."
)
def install_wifi_hotspot(
ssid: str, password: str, autoconnect: str, autoconnect_priority: str
):
"""Install Wi-Fi hotspot using NetworkManager.
For more details see: https://networkmanager.dev/docs/api/latest/
:return:
"""
click.echo("Installing Wi-Fi hotspot...")

# Enable NetworkManager
run_subprocess(["sudo", "systemctl", "enable", "NetworkManager"])

# Configure Wi-Fi hotspot with NetworkManager
run_subprocess(
[
"sudo",
"nmcli",
"connection",
"add",
"con-name",
WIFI_HOTSPOT_CONNECTION_NAME,
"type",
"wifi",
"hotspot",
"ifname",
"<Device>",
"ssid",
"IrrigationPi",
"password",
"password",
"wifi.mode",
"ap",
"wifi.ssid",
ssid,
"wifi-sec.key-mgmt",
"wpa-psk",
"wifi-sec.psk",
password,
"ipv4.method",
"shared",
"connection.autoconnect",
autoconnect,
"connection.autoconnect-priority",
autoconnect_priority,
]
)
17 changes: 17 additions & 0 deletions irrigation_pi/uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
NGINX_CONFIG_ACTIVATION_PATH,
NGINX_CONFIG_PATH,
SYSTEMD_CONFIG_PATH,
WIFI_HOTSPOT_CONNECTION_NAME,
)
from irrigation_pi.utils import (
run_subprocess,
Expand Down Expand Up @@ -80,3 +81,19 @@ def uninstall_nginx_configuration():

# Reload nginx config
run_subprocess(["sudo", "systemctl", "reload", "nginx"])


@click.command(name="wifi-hotspot")
def uninstall_wifi_hotspot():
"""Uninstall Wi-Fi hotspot using NetworkManager.
For more details see: https://networkmanager.dev/docs/api/latest/
:return:
"""
click.echo("Uninstalling Wi-Fi hotspot...")

# Delete Wi-Fi hotspot with NetworkManager
run_subprocess(
["sudo", "nmcli", "connection", "delete", WIFI_HOTSPOT_CONNECTION_NAME]
)

0 comments on commit 1e6649b

Please sign in to comment.