fix: one wrong sensor state topic; allow None (null) value to be sent

pull/106/head
Jeff Culverhouse 3 months ago
parent 1d52185673
commit f38251344e

@ -104,7 +104,7 @@ class AmcrestServiceProtocol(Protocol):
def mqtt_on_message(self, client: Client, userdata: Any, msg: MQTTMessage) -> None: ... def mqtt_on_message(self, client: Client, userdata: Any, msg: MQTTMessage) -> None: ...
def mqtt_on_subscribe(self, client: Client, userdata: Any, mid: int, reason_code_list: list[ReasonCode], properties: Properties) -> None: ... def mqtt_on_subscribe(self, client: Client, userdata: Any, mid: int, reason_code_list: list[ReasonCode], properties: Properties) -> None: ...
def mqtt_on_log(self, client: Client, userdata: Any, paho_log_level: int, msg: str) -> None: ... def mqtt_on_log(self, client: Client, userdata: Any, paho_log_level: int, msg: str) -> None: ...
def mqtt_safe_publish(self, topic: str, payload: str | bool | int | dict, **kwargs: Any) -> None: ... def mqtt_safe_publish(self, topic: str, payload: str | bool | int | dict | None, **kwargs: Any) -> None: ...
def mqttc_create(self) -> None: ... def mqttc_create(self) -> None: ...
def publish_device_availability(self, device_id: str, online: bool = True) -> None: ... def publish_device_availability(self, device_id: str, online: bool = True) -> None: ...
def publish_device_discovery(self, device_id: str) -> None: ... def publish_device_discovery(self, device_id: str) -> None: ...

@ -143,7 +143,7 @@ class AmcrestMixin:
"component_type": "sensor", "component_type": "sensor",
"name": "Recording url", "name": "Recording url",
"uniq_id": self.mqtt_helper.dev_unique_id(device_id, "recording_url"), "uniq_id": self.mqtt_helper.dev_unique_id(device_id, "recording_url"),
"stat_t": self.mqtt_helper.stat_t(device_id, "recording_url"), "stat_t": self.mqtt_helper.stat_t(device_id, "sensor", "recording_url"),
"avty_t": self.mqtt_helper.avty_t(device_id), "avty_t": self.mqtt_helper.avty_t(device_id),
"clip_url": f"media-source://media_source/local/Videos/amcrest/{device["device_name"]}-latest.mp4", "clip_url": f"media-source://media_source/local/Videos/amcrest/{device["device_name"]}-latest.mp4",
"icon": "mdi:web", "icon": "mdi:web",
@ -343,8 +343,8 @@ class AmcrestMixin:
sensor={ sensor={
"motion_region": "n/a", "motion_region": "n/a",
"event_text": "", "event_text": "",
"event_time": "unknown", "event_time": None,
"recording_time": "unknown", "recording_time": None,
"recording_url": "", "recording_url": "",
}, },
) )

@ -203,7 +203,7 @@ class MqttMixin:
joined = "; ".join(reason_names) if reason_names else "none" joined = "; ".join(reason_names) if reason_names else "none"
self.logger.debug(f"MQTT subscribed (mid={mid}): {joined}") self.logger.debug(f"MQTT subscribed (mid={mid}): {joined}")
def mqtt_safe_publish(self: Amcrest2Mqtt, topic: str, payload: str | bool | int | dict, **kwargs: Any) -> None: def mqtt_safe_publish(self: Amcrest2Mqtt, topic: str, payload: str | bool | int | dict | None, **kwargs: Any) -> None:
if not topic: if not topic:
raise ValueError("Cannot post to a blank topic") raise ValueError("Cannot post to a blank topic")
if isinstance(payload, dict) and ("component" in payload or "//////" in payload): if isinstance(payload, dict) and ("component" in payload or "//////" in payload):
@ -212,6 +212,9 @@ class MqttMixin:
self.logger.warning(f"payload: {payload}") self.logger.warning(f"payload: {payload}")
raise ValueError("Possible invalid payload. topic: {topic} payload: {payload}") raise ValueError("Possible invalid payload. topic: {topic} payload: {payload}")
try: try:
self.mqttc.publish(topic, cast(PayloadType, payload), **kwargs) if payload is None:
self.mqttc.publish(topic, "null", **kwargs)
else:
self.mqttc.publish(topic, cast(PayloadType, payload), **kwargs)
except Exception as e: except Exception as e:
self.logger.warning(f"MQTT publish failed for {topic} with {payload[:120] if isinstance(payload, str) else payload}: {e}") self.logger.warning(f"MQTT publish failed for {topic} with {payload[:120] if isinstance(payload, str) else payload}: {e}")

Loading…
Cancel
Save