fix model on rpi 0
This commit is contained in:
@@ -16,10 +16,9 @@ import os
|
|||||||
# get device host name - used in mqtt topic
|
# get device host name - used in mqtt topic
|
||||||
hostname = socket.gethostname()
|
hostname = socket.gethostname()
|
||||||
|
|
||||||
|
|
||||||
def check_wifi_signal():
|
def check_wifi_signal():
|
||||||
try:
|
try:
|
||||||
full_cmd = "/sbin/iwconfig wlan0 | grep -i quality"
|
full_cmd = "iwconfig wlan0 | grep -i --color quality"
|
||||||
wifi_signal = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
wifi_signal = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||||
wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[1].split('=')[1].split('/')[0]
|
wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[1].split('=')[1].split('/')[0]
|
||||||
wifi_signal_calc = round((int(wifi_signal) / 70)* 100)
|
wifi_signal_calc = round((int(wifi_signal) / 70)* 100)
|
||||||
@@ -28,16 +27,6 @@ def check_wifi_signal():
|
|||||||
return wifi_signal_calc
|
return wifi_signal_calc
|
||||||
|
|
||||||
|
|
||||||
def check_wifi_signal_dbm():
|
|
||||||
try:
|
|
||||||
full_cmd = "/sbin/iwconfig wlan0 | grep -i quality"
|
|
||||||
wifi_signal = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
|
||||||
wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[4].split('=')[1]
|
|
||||||
except Exception:
|
|
||||||
wifi_signal = 'NA'
|
|
||||||
return wifi_signal
|
|
||||||
|
|
||||||
|
|
||||||
def check_used_space(path):
|
def check_used_space(path):
|
||||||
st = os.statvfs(path)
|
st = os.statvfs(path)
|
||||||
free_space = st.f_bavail * st.f_frsize
|
free_space = st.f_bavail * st.f_frsize
|
||||||
@@ -100,8 +89,8 @@ def check_uptime():
|
|||||||
return int(subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0])
|
return int(subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0])
|
||||||
|
|
||||||
def check_model_name():
|
def check_model_name():
|
||||||
full_cmd = "cat /proc/cpuinfo | grep Model | sed 's/Model.*: //g'"
|
full_cmd = "cat /sys/firmware/devicetree/base/model"
|
||||||
return subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0].decode("utf-8")
|
return subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
def config_json(what_config):
|
def config_json(what_config):
|
||||||
@@ -158,10 +147,6 @@ def config_json(what_config):
|
|||||||
data["icon"] = "mdi:wifi"
|
data["icon"] = "mdi:wifi"
|
||||||
data["name"] = hostname + " Wifi Signal"
|
data["name"] = hostname + " Wifi Signal"
|
||||||
data["unit_of_measurement"] = "%"
|
data["unit_of_measurement"] = "%"
|
||||||
elif what_config == "wifi_signal_dbm":
|
|
||||||
data["icon"] = "mdi:wifi"
|
|
||||||
data["name"] = hostname + " Wifi Signal"
|
|
||||||
data["unit_of_measurement"] = "dBm"
|
|
||||||
else:
|
else:
|
||||||
return ""
|
return ""
|
||||||
# Return our built discovery config
|
# Return our built discovery config
|
||||||
@@ -169,7 +154,7 @@ def config_json(what_config):
|
|||||||
|
|
||||||
|
|
||||||
def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
|
def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
|
||||||
uptime_days=0, wifi_signal=0, wifi_signal_dbm=0):
|
uptime_days=0, wifi_signal=0):
|
||||||
# connect to mqtt server
|
# connect to mqtt server
|
||||||
client = paho.Client()
|
client = paho.Client()
|
||||||
client.username_pw_set(config.mqtt_user, config.mqtt_password)
|
client.username_pw_set(config.mqtt_user, config.mqtt_password)
|
||||||
@@ -240,24 +225,15 @@ def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_s
|
|||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/wifi_signal", wifi_signal, qos=1)
|
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/wifi_signal", wifi_signal, qos=1)
|
||||||
time.sleep(config.sleep_time)
|
time.sleep(config.sleep_time)
|
||||||
if config.wifi_signal_dbm:
|
|
||||||
if config.discovery_messages:
|
|
||||||
client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_wifi_signal_dbm/config",
|
|
||||||
config_json('wifi_signal_dbm'), qos=0)
|
|
||||||
time.sleep(config.sleep_time)
|
|
||||||
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/wifi_signal_dbm", wifi_signal_dbm, qos=1)
|
|
||||||
time.sleep(config.sleep_time)
|
|
||||||
|
|
||||||
|
|
||||||
# disconnect from mqtt server
|
# disconnect from mqtt server
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|
||||||
|
|
||||||
def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
|
def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
|
||||||
uptime_days=0, wifi_signal=0, wifi_signal_dbm=0):
|
uptime_days=0, wifi_signal=0):
|
||||||
# compose the CSV message containing the measured values
|
# compose the CSV message containing the measured values
|
||||||
|
|
||||||
values = cpu_load, float(cpu_temp), used_space, float(voltage), int(sys_clock_speed), swap, memory, uptime_days, wifi_signal, wifi_signal_dbm
|
values = cpu_load, float(cpu_temp), used_space, float(voltage), int(sys_clock_speed), swap, memory, uptime_days, wifi_signal
|
||||||
values = str(values)[1:-1]
|
values = str(values)[1:-1]
|
||||||
|
|
||||||
# connect to mqtt server
|
# connect to mqtt server
|
||||||
@@ -274,7 +250,7 @@ def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_cl
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# set all monitored values to False in case they are turned off in the config
|
# set all monitored values to False in case they are turned off in the config
|
||||||
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = wifi_signal_dbm = False
|
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = False
|
||||||
|
|
||||||
# delay the execution of the script
|
# delay the execution of the script
|
||||||
time.sleep(config.random_delay)
|
time.sleep(config.random_delay)
|
||||||
@@ -298,10 +274,8 @@ if __name__ == '__main__':
|
|||||||
uptime_days = check_uptime()
|
uptime_days = check_uptime()
|
||||||
if config.wifi_signal:
|
if config.wifi_signal:
|
||||||
wifi_signal = check_wifi_signal()
|
wifi_signal = check_wifi_signal()
|
||||||
if config.wifi_signal_dbm:
|
|
||||||
wifi_signal_dbm = check_wifi_signal_dbm()
|
|
||||||
# Publish messages to MQTT
|
# Publish messages to MQTT
|
||||||
if config.group_messages:
|
if config.group_messages:
|
||||||
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm)
|
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal)
|
||||||
else:
|
else:
|
||||||
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm)
|
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal)
|
||||||
|
|||||||
Reference in New Issue
Block a user