diff --git a/src/amcrest2mqtt/interface.py b/src/amcrest2mqtt/interface.py index 9552ef6..93e1f35 100644 --- a/src/amcrest2mqtt/interface.py +++ b/src/amcrest2mqtt/interface.py @@ -104,7 +104,7 @@ class AmcrestServiceProtocol(Protocol): 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_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 publish_device_availability(self, device_id: str, online: bool = True) -> None: ... def publish_device_discovery(self, device_id: str) -> None: ... diff --git a/src/amcrest2mqtt/mixins/amcrest.py b/src/amcrest2mqtt/mixins/amcrest.py index a74d754..d6fea1a 100644 --- a/src/amcrest2mqtt/mixins/amcrest.py +++ b/src/amcrest2mqtt/mixins/amcrest.py @@ -143,7 +143,7 @@ class AmcrestMixin: "component_type": "sensor", "name": "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), "clip_url": f"media-source://media_source/local/Videos/amcrest/{device["device_name"]}-latest.mp4", "icon": "mdi:web", @@ -343,8 +343,8 @@ class AmcrestMixin: sensor={ "motion_region": "n/a", "event_text": "", - "event_time": "unknown", - "recording_time": "unknown", + "event_time": None, + "recording_time": None, "recording_url": "", }, ) diff --git a/src/amcrest2mqtt/mixins/mqtt.py b/src/amcrest2mqtt/mixins/mqtt.py index 6f738e8..3022603 100644 --- a/src/amcrest2mqtt/mixins/mqtt.py +++ b/src/amcrest2mqtt/mixins/mqtt.py @@ -203,7 +203,7 @@ class MqttMixin: joined = "; ".join(reason_names) if reason_names else "none" 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: raise ValueError("Cannot post to a blank topic") if isinstance(payload, dict) and ("component" in payload or "//////" in payload): @@ -212,6 +212,9 @@ class MqttMixin: self.logger.warning(f"payload: {payload}") raise ValueError("Possible invalid payload. topic: {topic} payload: {payload}") 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: self.logger.warning(f"MQTT publish failed for {topic} with {payload[:120] if isinstance(payload, str) else payload}: {e}")