From 41ec32af471c25e4e65451363e09c83099956140 Mon Sep 17 00:00:00 2001 From: Jeff Culverhouse Date: Mon, 24 Nov 2025 10:01:46 -0500 Subject: [PATCH] fix: always try to log device_name in preference to device_id --- src/amcrest2mqtt/mixins/amcrest.py | 10 +++++++--- src/amcrest2mqtt/mixins/amcrest_api.py | 18 +++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/amcrest2mqtt/mixins/amcrest.py b/src/amcrest2mqtt/mixins/amcrest.py index 521894f..ae37d23 100644 --- a/src/amcrest2mqtt/mixins/amcrest.py +++ b/src/amcrest2mqtt/mixins/amcrest.py @@ -26,7 +26,11 @@ class AmcrestMixin: device_name = device.get("device_name", "unknown") device_id = device.get("serial_number", "unknown") exception_type = type(result).__name__ - self.logger.error(f"error during build_component for device '{device_name}' ({device_id}): " f"{exception_type}: {result}", exc_info=True) + try: + device_display_name = self.get_device_name(device_id) + except KeyError: + device_display_name = device_id + self.logger.error(f"error during build_component for device '{device_name}' ({device_display_name}): " f"{exception_type}: {result}", exc_info=True) elif result and isinstance(result, str): seen_devices.add(result) @@ -34,7 +38,7 @@ class AmcrestMixin: missing_devices = set(self.devices.keys()) - seen_devices for device_id in missing_devices: await self.publish_device_availability(device_id, online=False) - self.logger.warning(f"device {device_id} not seen in Amcrest API list — marked offline") + self.logger.warning(f"device {self.get_device_name(device_id)} not seen in Amcrest API list — marked offline") # Handle first discovery completion if not self.discovery_complete: @@ -275,7 +279,7 @@ class AmcrestMixin: ) if not self.is_discovered(device_id): - self.logger.info(f'added new camera: "{camera["device_name"]}" {camera["vendor"]} {camera["device_type"]}] ({device_id})') + self.logger.info(f'added new camera: "{camera["device_name"]}" {camera["vendor"]} {camera["device_type"]}] ({self.get_device_name(device_id)})') await self.publish_device_discovery(device_id) await self.publish_device_availability(device_id, online=True) diff --git a/src/amcrest2mqtt/mixins/amcrest_api.py b/src/amcrest2mqtt/mixins/amcrest_api.py index 53396e7..76f9301 100644 --- a/src/amcrest2mqtt/mixins/amcrest_api.py +++ b/src/amcrest2mqtt/mixins/amcrest_api.py @@ -151,7 +151,7 @@ class AmcrestAPIMixin: def reboot_device(self: Amcrest2Mqtt, device_id: str) -> None: if device_id not in self.amcrest_devices: - self.logger.warning(f"device not found for {device_id}") + self.logger.warning(f"device not found for {self.get_device_name(device_id)}") return None device = self.amcrest_devices[device_id] if not device["camera"]: @@ -185,7 +185,7 @@ class AmcrestAPIMixin: async def get_storage_stats(self: Amcrest2Mqtt, device_id: str) -> dict[str, str | float]: if device_id not in self.amcrest_devices: - self.logger.warning(f"device not found for {device_id}") + self.logger.warning(f"device not found for {self.get_device_name(device_id)}") return {} device = self.amcrest_devices[device_id] @@ -227,7 +227,7 @@ class AmcrestAPIMixin: async def get_privacy_mode(self: Amcrest2Mqtt, device_id: str) -> bool: if device_id not in self.amcrest_devices: - self.logger.warning(f"device not found for {device_id}") + self.logger.warning(f"device not found for {self.get_device_name(device_id)}") return False device = self.amcrest_devices[device_id] @@ -274,7 +274,7 @@ class AmcrestAPIMixin: async def set_privacy_mode(self: Amcrest2Mqtt, device_id: str, switch: bool) -> None: if device_id not in self.amcrest_devices: - self.logger.warning(f"device not found for {device_id}") + self.logger.warning(f"device not found for {self.get_device_name(device_id)}") return None device = self.amcrest_devices[device_id] @@ -303,7 +303,7 @@ class AmcrestAPIMixin: async def get_motion_detection(self: Amcrest2Mqtt, device_id: str) -> bool: if device_id not in self.amcrest_devices: - self.logger.warning(f"device not found for {device_id}") + self.logger.warning(f"device not found for {self.get_device_name(device_id)}") return False device = self.amcrest_devices[device_id] @@ -330,7 +330,7 @@ class AmcrestAPIMixin: async def set_motion_detection(self: Amcrest2Mqtt, device_id: str, switch: bool) -> None: if device_id not in self.amcrest_devices: - self.logger.warning(f"device not found for {device_id}") + self.logger.warning(f"device not found for {self.get_device_name(device_id)}") return None device = self.amcrest_devices[device_id] @@ -357,7 +357,7 @@ class AmcrestAPIMixin: async def get_snapshot_from_device(self: Amcrest2Mqtt, device_id: str) -> str | None: if device_id not in self.amcrest_devices: - self.logger.warning(f"device not found for {device_id}") + self.logger.warning(f"device not found for {self.get_device_name(device_id)}") return None device = self.amcrest_devices[device_id] @@ -418,7 +418,7 @@ class AmcrestAPIMixin: async def get_recorded_file(self: Amcrest2Mqtt, device_id: str, file: str, encode: bool = True) -> str | None: if device_id not in self.amcrest_devices: - self.logger.warning(f"device not found for {device_id}") + self.logger.warning(f"device not found for {self.get_device_name(device_id)}") return None device = self.amcrest_devices[device_id] @@ -488,7 +488,7 @@ class AmcrestAPIMixin: async def process_device_event(self: Amcrest2Mqtt, device_id: str, code: str, payload: Any) -> None: if device_id not in self.amcrest_devices: - self.logger.warning(f"device not found for {device_id}") + self.logger.warning(f"device not found for {self.get_device_name(device_id)}") return None device = self.amcrest_devices[device_id]