Skip to content

Commit 93ae351

Browse files
committed
Add speaker used module
1 parent 78d312d commit 93ae351

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

lnxlink/modules/speaker_used.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Checks if the Speaker is used"""
2+
import glob
3+
import json
4+
import re
5+
from .scripts.helpers import syscommand
6+
7+
8+
class Addon:
9+
"""Addon module"""
10+
11+
def __init__(self, lnxlink):
12+
"""Setup addon"""
13+
self.name = "Speaker used"
14+
self.lnxlink = lnxlink
15+
16+
_, _, returncode = syscommand(
17+
"which pactl && pactl -f json list short sink-inputs", ignore_errors=True
18+
)
19+
self.use_pactl = False
20+
if returncode == 0:
21+
self.use_pactl = True
22+
23+
def get_info(self):
24+
"""Gather information from the system"""
25+
if self.use_pactl:
26+
stdout, _, _ = syscommand(
27+
"pactl -f json list short sink-inputs",
28+
)
29+
# Replace 0,00 values with 0.00
30+
stdout = re.sub(r"(\s*[+-]?[0-9]+),([0-9]+\s*)", r"\1.\2", stdout)
31+
data = json.loads(stdout)
32+
if len(data) > 0:
33+
return "ON"
34+
return "OFF"
35+
speakers = glob.glob("/proc/asound/card*/pcm*/sub*/status", recursive=True)
36+
for speaker in speakers:
37+
with open(speaker, encoding="UTF-8") as speaker_content:
38+
speaker_status = speaker_content.read().strip().lower()
39+
if speaker_status != "closed":
40+
return "ON"
41+
return "OFF"
42+
43+
def exposed_controls(self):
44+
"""Exposes to home assistant"""
45+
return {
46+
"Speaker used": {
47+
"type": "binary_sensor",
48+
"icon": "mdi:speaker",
49+
},
50+
}

0 commit comments

Comments
 (0)