Skip to content

Commit

Permalink
Disks show if connected and run auto discover on new device
Browse files Browse the repository at this point in the history
  • Loading branch information
bkbilly committed Aug 24, 2023
1 parent 9ed1c0d commit 2dfab71
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
10 changes: 2 additions & 8 deletions lnxlink/config_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ def userprompt_config(config_path):
"This will update the MQTT credentials and topics, these are the default topics:"
)
logger.info(
" MQTT Topic prefix for for monitoring: %s/%s/%s/...",
" MQTT Topic prefix for for monitoring: %s/%s/...",
config["mqtt"]["prefix"],
config["mqtt"]["clientId"],
config["mqtt"]["statsPrefix"],
)
logger.info(
" MQTT Topic prefix for for commands: %s/%s/commands/...",
Expand Down Expand Up @@ -114,20 +113,15 @@ def userprompt_config(config_path):
input(f" Change clientId [{config['mqtt']['clientId']}]: ")
or config["mqtt"]["clientId"]
)
config["mqtt"]["statsPrefix"] = (
input(f" Change statsPrefix [{config['mqtt']['statsPrefix']}]: ")
or config["mqtt"]["statsPrefix"]
)

with open(config_path, "w", encoding="UTF-8") as file:
file.write(yaml.dump(config, default_flow_style=False, sort_keys=False))

logger.info("\nAll changes have been saved.")
logger.info(
" MQTT Topic prefix for for monitoring: %s/%s/%s/...",
" MQTT Topic prefix for for monitoring: %s/%s/...",
config["mqtt"]["prefix"],
config["mqtt"]["clientId"],
config["mqtt"]["statsPrefix"],
)
logger.info(
" MQTT Topic prefix for for commands: %s/%s/commands/...",
Expand Down
30 changes: 21 additions & 9 deletions lnxlink/modules/disk_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ class Addon:
def __init__(self, lnxlink):
"""Setup addon"""
self.name = "Disk Usage"
self.lnxlink = lnxlink
self.disks = self._get_disks()

def exposed_controls(self):
"""Exposes to home assistant"""
discovery_info = {}
for disk in psutil.disk_partitions():
if disk.fstype == "squashfs":
continue
if "docker/overlay" in disk.mountpoint:
continue
device = disk.device.replace("/", "_").strip("_")
for device in self.disks:
discovery_info[f"Disk {device}"] = {
"type": "sensor",
"icon": "mdi:harddisk",
Expand All @@ -31,6 +28,23 @@ def exposed_controls(self):

def get_info(self):
"""Gather information from the system"""
disks = self._get_disks()
mounted = set(disks) - set(self.disks)
unmounted = set(self.disks) - set(disks)
# print(mounted, unmounted)
for disk_name in unmounted:
disks[disk_name] = self.disks[disk_name]
self.disks[disk_name]['connected'] = False
self.disks = disks
for disk_name in mounted:
self.lnxlink.setup_discovery()
return self.disks

def _bytetomb(self, byte):
return round(byte / 1024 / 1024, 1)

def _get_disks(self):
"""Get a list of all disks"""
disks = {}
for disk in psutil.disk_partitions():
if disk.fstype == "squashfs":
Expand All @@ -44,7 +58,5 @@ def get_info(self):
disks[device]["used"] = self._bytetomb(disk_stats.used)
disks[device]["free"] = self._bytetomb(disk_stats.free)
disks[device]["percent"] = disk_stats.percent
disks[device]["connected"] = True
return disks

def _bytetomb(self, byte):
return round(byte / 1024 / 1024, 1)

0 comments on commit 2dfab71

Please sign in to comment.