Skip to content

Commit

Permalink
Add sensors using bash module
Browse files Browse the repository at this point in the history
  • Loading branch information
bkbilly committed May 31, 2024
1 parent d2cd61a commit e02093b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lnxlink/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def run_module(self, name, method):

try:
start_time = time.time()
if isinstance(method, (dict, list, bool, bytes)):
if isinstance(method, (dict, list, bool, bytes, int, str, float)):
pub_data = method
else:
pub_data = method()
Expand Down
35 changes: 28 additions & 7 deletions lnxlink/modules/bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ def __init__(self, lnxlink):
"""Setup addon"""
self.name = "bash"
self.lnxlink = lnxlink
self.discovery_info = {}

def get_info(self):
"""Gather information from the system"""
for expose_name, discovery in self.discovery_info.items():
if discovery.get("type") == "sensor":
stdout, _, returncode = syscommand(discovery["local_command"])
if returncode == 0:
self.lnxlink.run_module(f"{self.name}/{expose_name}", stdout)

def exposed_controls(self):
"""Exposes to home assistant"""
discovery_info = {
self.discovery_info = {
"Bash Command": {
"type": "text",
"icon": "mdi:bash",
Expand All @@ -21,12 +30,24 @@ def exposed_controls(self):
exposed = self.lnxlink.config["settings"]["bash"]["expose"]
exposed = [] if exposed is None else exposed
for expose in exposed:
discovery_info[f"Bash {expose['name']}"] = {
"type": "button",
"icon": expose.get("icon", "mdi:script-text"),
"payload_press": expose["command"],
}
return discovery_info
expose_type = expose.get("type", "button")
expose_name = f"Bash {expose['name']}"
icon = expose.get("icon", "mdi:script-text")
if expose_type == "button":
self.discovery_info[expose_name] = {
"type": expose_type,
"icon": icon,
"payload_press": expose["command"],
}
elif expose_type == "sensor":
self.discovery_info[expose_name] = {
"type": expose_type,
"icon": icon,
"unit": expose.get("unit"),
"local_command": expose.get("command"),
"subtopic": True,
}
return self.discovery_info

def start_control(self, topic, data):
"""Control system"""
Expand Down

0 comments on commit e02093b

Please sign in to comment.