add wifi signal strength sensor
parent
0bb857a6b3
commit
9906b132bd
|
@ -14,6 +14,7 @@
|
||||||
src = ./src;
|
src = ./src;
|
||||||
propagatedBuildInputs = with pkgs.python311Packages; [
|
propagatedBuildInputs = with pkgs.python311Packages; [
|
||||||
pkgs.light
|
pkgs.light
|
||||||
|
pkgs.wirelesstools
|
||||||
pkgs.xorg.xset
|
pkgs.xorg.xset
|
||||||
|
|
||||||
coloredlogs
|
coloredlogs
|
||||||
|
@ -28,6 +29,7 @@
|
||||||
name = "development shell";
|
name = "development shell";
|
||||||
nativeBuildInputs = with pkgs.python311Packages; [
|
nativeBuildInputs = with pkgs.python311Packages; [
|
||||||
pkgs.light
|
pkgs.light
|
||||||
|
pkgs.wirelesstools
|
||||||
pkgs.xorg.xset
|
pkgs.xorg.xset
|
||||||
|
|
||||||
coloredlogs
|
coloredlogs
|
||||||
|
|
|
@ -68,7 +68,8 @@ async def main():
|
||||||
'display_brightness': sensor.DisplayBrightnessSensor(device, dbm),
|
'display_brightness': sensor.DisplayBrightnessSensor(device, dbm),
|
||||||
'ip_address': sensor.IPAddressSensor(device),
|
'ip_address': sensor.IPAddressSensor(device),
|
||||||
'last_boot': sensor.LastBootSensor(device),
|
'last_boot': sensor.LastBootSensor(device),
|
||||||
'temperature': sensor.TemperatureSensor(device)
|
'temperature': sensor.TemperatureSensor(device),
|
||||||
|
'wifi_signal': sensor.WifiSignalStrengthSensor(device)
|
||||||
};
|
};
|
||||||
|
|
||||||
tasks.append(sensor_loop(logger, sensor_entities))
|
tasks.append(sensor_loop(logger, sensor_entities))
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
import psutil
|
import psutil
|
||||||
|
import re
|
||||||
import socket
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
@ -29,7 +30,8 @@ class BatterySensor():
|
||||||
charger_connected_info = ha_mqtt_discoverable.sensors.BinarySensorInfo(
|
charger_connected_info = ha_mqtt_discoverable.sensors.BinarySensorInfo(
|
||||||
name="Battery charger connected",
|
name="Battery charger connected",
|
||||||
device=device.get_mqtt_device(),
|
device=device.get_mqtt_device(),
|
||||||
unique_id=f'{device.get_device_id()}_battery_charger_connected')
|
unique_id=f'{device.get_device_id()}_battery_charger_connected',
|
||||||
|
device_class='plug')
|
||||||
charger_connected_settings = ha_mqtt_discoverable.Settings(mqtt=device.get_mqtt_settings(), entity=charger_connected_info, manual_availability=True)
|
charger_connected_settings = ha_mqtt_discoverable.Settings(mqtt=device.get_mqtt_settings(), entity=charger_connected_info, manual_availability=True)
|
||||||
self.connected_entity = ha_mqtt_discoverable.sensors.BinarySensor(charger_connected_settings)
|
self.connected_entity = ha_mqtt_discoverable.sensors.BinarySensor(charger_connected_settings)
|
||||||
self.connected_entity.set_availability(True)
|
self.connected_entity.set_availability(True)
|
||||||
|
@ -207,3 +209,32 @@ class TemperatureSensor():
|
||||||
|
|
||||||
self.value = newValue
|
self.value = newValue
|
||||||
|
|
||||||
|
class WifiSignalStrengthSensor():
|
||||||
|
value = -1
|
||||||
|
mqtt_sensor = None
|
||||||
|
|
||||||
|
def __init__(self, device: Device):
|
||||||
|
wifi_strength_info = ha_mqtt_discoverable.sensors.SensorInfo(
|
||||||
|
name="Wifi Signal Strength",
|
||||||
|
device=device.get_mqtt_device(),
|
||||||
|
unique_id=f'{device.get_device_id()}_wifi_signal_strength',
|
||||||
|
device_class='signal_strength',
|
||||||
|
unit_of_measurement='dBm')
|
||||||
|
wifi_strength_settings = ha_mqtt_discoverable.Settings(mqtt=device.get_mqtt_settings(), entity=wifi_strength_info, manual_availability=True)
|
||||||
|
self.mqtt_sensor = ha_mqtt_discoverable.sensors.Sensor(wifi_strength_settings)
|
||||||
|
self.update_value()
|
||||||
|
|
||||||
|
def update_value(self):
|
||||||
|
res = subprocess.run(['iwconfig'], capture_output=True)
|
||||||
|
m = re.findall('Signal level=(-[0-9]+) dBm', res.stdout.decode('utf-8'))
|
||||||
|
if len(m) == 0:
|
||||||
|
self.mqtt_sensor.set_availability(False)
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
self.mqtt_sensor.set_availability(True)
|
||||||
|
|
||||||
|
newValue = float(m[0])
|
||||||
|
if newValue != self.value:
|
||||||
|
self.mqtt_sensor.set_state(newValue)
|
||||||
|
|
||||||
|
self.value = newValue
|
||||||
|
|
Loading…
Reference in New Issue