Version 2 Player
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
"""Home Assistant MQTT discovery publishing for the video player.
|
||||
|
||||
This module deliberately reuses the player's existing MQTT client. It does
|
||||
not create another connection or change any of the player's MQTT topics.
|
||||
"""
|
||||
|
||||
import json
|
||||
from typing import Any, Dict
|
||||
|
||||
|
||||
def _publish_sensor(
|
||||
client: Any,
|
||||
*,
|
||||
discovery_prefix: str,
|
||||
device_id: str,
|
||||
device_name: str,
|
||||
telemetry_topic: str,
|
||||
status_topic: str,
|
||||
entity_id: str,
|
||||
name: str,
|
||||
value_template: str,
|
||||
icon: str = "",
|
||||
unit: str = "",
|
||||
) -> None:
|
||||
topic = f"{discovery_prefix}/sensor/{device_id}/{entity_id}/config"
|
||||
payload: Dict[str, Any] = {
|
||||
"name": name,
|
||||
"unique_id": f"{device_id}_{entity_id}",
|
||||
"state_topic": telemetry_topic,
|
||||
"value_template": value_template,
|
||||
"availability_topic": status_topic,
|
||||
"availability_template": "{% if value_json.state != 'offline' %}online{% else %}offline{% endif %}",
|
||||
"payload_available": "online",
|
||||
"payload_not_available": "offline",
|
||||
"device": {
|
||||
"identifiers": [device_id],
|
||||
"name": device_name,
|
||||
"manufacturer": "Custom",
|
||||
"model": "mpv mqtt player",
|
||||
},
|
||||
}
|
||||
|
||||
if icon:
|
||||
payload["icon"] = icon
|
||||
if unit:
|
||||
payload["unit_of_measurement"] = unit
|
||||
|
||||
client.publish(topic, json.dumps(payload), qos=1, retain=True)
|
||||
|
||||
|
||||
def publish_discovery(
|
||||
client: Any,
|
||||
*,
|
||||
discovery_prefix: str,
|
||||
device_id: str,
|
||||
device_name: str,
|
||||
telemetry_topic: str,
|
||||
status_topic: str,
|
||||
) -> None:
|
||||
"""Publish the same nine retained discovery sensors used historically."""
|
||||
sensors = (
|
||||
("position", "YT Position (s)", "{{ value_json.position }}", "mdi:timeline-clock", "s"),
|
||||
("duration", "YT Duration (s)", "{{ value_json.duration }}", "mdi:timer", "s"),
|
||||
("remaining", "YT Remaining (s)", "{{ value_json.remaining }}", "mdi:timer-sand", "s"),
|
||||
("position_fmt", "YT Position", "{{ value_json.position_fmt }}", "mdi:timeline-clock", ""),
|
||||
("duration_fmt", "YT Duration", "{{ value_json.duration_fmt }}", "mdi:timer", ""),
|
||||
("remaining_fmt", "YT Remaining", "{{ value_json.remaining_fmt }}", "mdi:timer-sand", ""),
|
||||
("title", "YT Title", "{{ value_json.title }}", "mdi:music", ""),
|
||||
("paused", "YT Paused", "{{ value_json.paused }}", "mdi:pause-circle", ""),
|
||||
("slot", "YT Slot", "{{ value_json.slot }}", "mdi:playlist-play", ""),
|
||||
)
|
||||
|
||||
for entity_id, name, template, icon, unit in sensors:
|
||||
_publish_sensor(
|
||||
client,
|
||||
discovery_prefix=discovery_prefix,
|
||||
device_id=device_id,
|
||||
device_name=device_name,
|
||||
telemetry_topic=telemetry_topic,
|
||||
status_topic=status_topic,
|
||||
entity_id=entity_id,
|
||||
name=name,
|
||||
value_template=template,
|
||||
icon=icon,
|
||||
unit=unit,
|
||||
)
|
||||
Reference in New Issue
Block a user