Skip to content

Commit

Permalink
Add bash switch option
Browse files Browse the repository at this point in the history
  • Loading branch information
bkbilly committed Jul 16, 2024
1 parent cf01aed commit 4c42104
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lnxlink/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,10 @@ def setup_discovery_entities(self, addon, service, exp_name, options):
"switch": {
"state_topic": state_topic,
"command_topic": command_topic,
"payload_off": "OFF",
"payload_on": "ON",
"payload_off": options.get("command_off", "OFF"),
"payload_on": options.get("command_on", "ON"),
"state_off": "OFF",
"state_on": "ON",
},
"text": {
"state_topic": state_topic,
Expand Down
24 changes: 22 additions & 2 deletions lnxlink/modules/bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ def get_info(self):
if returncode == 0:
self.lnxlink.run_module(f"{self.name}/{expose_name}", stdout)
elif discovery.get("type") == "binary_sensor":
stdout, _, returncode = syscommand(discovery["local_command"])
stdout, _, _ = syscommand(discovery["local_command"])
status = stdout.lower() not in ["false", "no", "0", ""]
senddata = {
"status": "ON" if status else "OFF",
"attributes": {"raw": stdout.split("\n")},
}
self.lnxlink.run_module(f"{self.name}/{expose_name}", senddata)
elif discovery.get("type") == "switch":
stdout, _, _ = syscommand(
discovery["local_command"], ignore_errors=True
)
status = stdout.lower() not in ["false", "no", "0", ""]
self.lnxlink.run_module(f"{self.name}/{expose_name}", status)

def exposed_controls(self):
"""Exposes to home assistant"""
Expand Down Expand Up @@ -68,6 +74,15 @@ def exposed_controls(self):
"local_command": expose.get("command"),
"subtopic": True,
}
elif expose_type == "switch":
self.discovery_info[expose_name] = {
"type": expose_type,
"icon": icon,
"local_command": expose.get("command"),
"command_on": expose.get("command_on"),
"command_off": expose.get("command_off"),
"subtopic": True,
}
if expose.get("entity_category") in ["diagnostic", "config"]:
self.discovery_info[expose_name]["entity_category"] = expose[
"entity_category"
Expand All @@ -84,7 +99,12 @@ def start_control(self, topic, data):
exposed = self.lnxlink.config["settings"]["bash"]["expose"]
exposed = [] if exposed is None else exposed
for expose in exposed:
if data.strip() == expose.get("command", "").strip():
command_list = [
expose.get("command", "").strip(),
expose.get("command_on", "").strip(),
expose.get("command_off", "").strip(),
]
if data.strip() in command_list:
stdout, _, _ = syscommand(data, timeout=120)
return stdout
logger.error(
Expand Down

0 comments on commit 4c42104

Please sign in to comment.