consolidate commands topic

This commit is contained in:
2026-09-18 14:39:25 +02:00
parent 8f146678de
commit d35b7d53fd
5 changed files with 23 additions and 19 deletions
+3 -1
View File
@@ -67,7 +67,9 @@
"MQTT_USER": "projmon",
"MQTT_PASSWORD": "projmon",
"MQTT_HOST": "192.168.31.26",
"MQTT_PORT": "1883"
"MQTT_PORT": "1883",
"MQTT_COMMAND_TOPIC": "projmon/commands",
"MQTT_RESPONSE_TOPIC": "projmon/responses"
}
}
]
+2
View File
@@ -171,6 +171,8 @@ services:
- MQTT_PASSWORD=projmon
- MQTT_HOST=192.168.31.26
- MQTT_PORT=1883
- MQTT_COMMAND_TOPIC=projmon/commands
- MQTT_RESPONSE_TOPIC=projmon/responses
- LOG_FILE=/tmp/projmon.log
- LOG_FILE_LVL=WARNING
- LOG_CLI_LVL=INFO
+2 -2
View File
@@ -66,7 +66,7 @@ def initialize_ftp_servers(file_path: str) -> FTPServers:
name="MART",
host="192.168.31.15",
port=21,
notify_filters=["TLR", "TRL", "FTR"],
notify_filters=["FTR"],
cwd='TRAILER',
recurse=True
))
@@ -77,7 +77,7 @@ def initialize_ftp_servers(file_path: str) -> FTPServers:
port=21,
username="dcpftp",
password="dcpftp",
notify_filters=["TLR", "TRL", "FTR"]
notify_filters=["FTR"]
))
save_ftp_servers_to_json(dcp_servers, file_path)
-1
View File
@@ -26,7 +26,6 @@ class HAbutton(HAentity):
payload_press: str = ""
device_class: Optional[str] = None
icon: Optional[str] = None
callback: Optional[Callable[[mqtt_client.Client, Any, mqtt_client.MQTTMessage], None]] = Field(exclude=True, default=None)
class HAdevice(BaseModel):
identifiers: List[str]
+16 -15
View File
@@ -28,6 +28,8 @@ INTERVAL = int(env['INTERVAL'])
DCP_INTERVAL = int(env['DCP_INTERVAL'])
DCP_SERVER_FILE = str(env['DCP_SERVER_FILE'])
DCP_NEW_TOPIC = str(env['DCP_NEW_TOPIC'])
MQTT_COMMAND_TOPIC = str(env['MQTT_COMMAND_TOPIC'])
MQTT_RESPONSE_TOPIC = str(env['MQTT_RESPONSE_TOPIC'])
projector: projrequest.ProjectorConnection = projrequest.ProjectorConnection(
ip=env['PROJECTOR_IP'],
@@ -48,9 +50,12 @@ def on_shutwown(client: mqtt_client.Client, userdata: Any, msg: mqtt_client.MQTT
def on_command(client: mqtt_client.Client, userdata: Any, msg: mqtt_client.MQTTMessage) -> None:
try:
content: Dict[str, Any] = json.loads(msg.payload.decode('utf-8'))
cmd: str = content["cmd"]
params: str = content["params"]
cmd: str = content.get("cmd", "")
params: Dict[str, Any] = content.get("params", {})
match cmd:
case "shutdown":
LOGGER.info("Shutting down projector")
projector.shutdown()
case "ingest":
notify: NotificationResponse = NotificationResponse.model_validate(params)
with open(DCP_SERVER_FILE, 'r') as fd:
@@ -72,13 +77,14 @@ def on_command(client: mqtt_client.Client, userdata: Any, msg: mqtt_client.MQTTM
data=SMSMessage(SMSMessage=ingest_command)
)
if not error_response.MessageBody:
LOGGER.warning("Invalid error response: [%s]", error_response.model_dump_json())
LOGGER.warning("Invalid error response:\n[%s]", error_response.model_dump_json())
return
if error_response.MessageBody.ErrorResponse.Code != 100:
LOGGER.error("Unable to ingest Title [%s]: [%s]", dir_name, error_response.MessageBody.ErrorResponse.Message)
else:
LOGGER.info("Started ingest of Title: [%s] from [%s]", dir_name, this_server.name)
case _:
LOGGER.error(f"Invalid or empty command: [{cmd}]")
except json.JSONDecodeError as de:
LOGGER.error("Unable to decode command json: [%s]", de)
except KeyError as ke:
@@ -117,11 +123,10 @@ def main() -> int:
sony_device.entities.append(HAbutton (
name="Power Off",
unique_id=f"{sony_device.short_name}_power_off",
command_topic=f"{sony_device.short_name}/shutdown",
command_topic=MQTT_COMMAND_TOPIC,
icon="mdi:power",
payload_press="shutdown",
payload_press=json.dumps({"cmd":"shutdown", "params":{}}),
entity_type="button",
callback=on_shutwown,
device=sony_device
))
@@ -304,16 +309,11 @@ def main() -> int:
mqtt.publish(topic=f"{DISCOVERY_PREFIX}/{s.entity_type}/{s.unique_id}/config",
payload=s.model_dump_json(indent=1, exclude_none=True),
retain=True)
# subscrie to command topic if this is a button
if isinstance(s, HAbutton):
mqtt.subscribe(topic=s.command_topic)
if s.callback is not None:
mqtt.message_callback_add(sub=s.command_topic, callback=s.callback)
time.sleep(0.2)
# Subscrie to MQTT topics for generic commands
mqtt.subscribe(topic=f"{sony_device.short_name}/commands")
mqtt.message_callback_add(sub=f"{sony_device.short_name}/commands", callback=on_command)
# Subscribe to MQTT topics for generic commands from buttons and other entities
mqtt.subscribe(topic=MQTT_COMMAND_TOPIC)
mqtt.message_callback_add(sub=MQTT_COMMAND_TOPIC, callback=on_command)
playlists: SPLList | None = None
while handler.running:
@@ -384,6 +384,7 @@ def main() -> int:
LOGGER.debug("Cycle Time: %4.3f", cycle_time)
if cycle_time < INTERVAL:
time.sleep(INTERVAL-cycle_time)
mqtt.disconnect()
return 0