Refactor MQTT player attributes to use binary_sensor and update value handling

This commit is contained in:
Edgar P. Burkhart 2025-03-10 10:51:14 +01:00
parent 661984ff17
commit 5c7c8836f7
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 6 additions and 4 deletions

View file

@ -331,9 +331,9 @@ class HassUserClient(HassClient):
},
"player": {
"unique_id": f"{self.node_id}_player",
"p": "sensor",
"p": "binary_sensor",
"name": "Player",
"icon": "mdi:music",
"device_class": "running",
"value_template": "{{ value_json.player.value }}",
"json_attributes_topic": self.state_topic,
"json_attributes_template": "{{ value_json.player.attributes | to_json }}",
@ -381,6 +381,7 @@ class HassUserClient(HassClient):
code, value = run_command(["playerctl", "status"])
attrs = dict()
if code == 0:
res = "ON" if value == "Playing" else "OFF"
for k in ["title", "album", "artist"]:
code, v = run_command(["playerctl", "metadata", k])
if code == 0:
@ -389,9 +390,10 @@ class HassUserClient(HassClient):
log.error(f"Failed to get metadata: {k}")
else:
log.debug("Player is not running")
res = "OFF"
return {
"value": value,
"value": res,
"attributes": attrs,
}

View file

@ -12,7 +12,7 @@ def run_command(cmd: list[str]) -> Tuple[int, str]:
if proc.returncode != 0:
return proc.returncode, ""
return proc.returncode, proc.stdout.decode("utf-8")
return proc.returncode, proc.stdout.decode("utf-8").strip()
def test_connection(host: str, port: int) -> bool: