NVR with realtime local object detection for IP cameras
TypeScript aicameragoogle-coralhome-assistanthome-automationhomeautomationmqttnvrobject-detectionrealtimertsptensorflow
Improve Notifications (#16453)
Josh Hawkins9a0211a71c4b
diff --git a/docs/docs/configuration/reference.md b/docs/docs/configuration/reference.mdindex 93ab40ee7d..65a0f4825f 100644--- a/docs/docs/configuration/reference.md+++ b/docs/docs/configuration/reference.md@@ -409,6 +409,7 @@ motion:mqtt_off_delay: 30# Optional: Notification Configuration+# NOTE: Can be overridden at the camera level (except email)notifications:# Optional: Enable notification service (default: shown below)enabled: Falsediff --git a/docs/docs/integrations/mqtt.md b/docs/docs/integrations/mqtt.mdindex 45d95c9f43..c344a5aaa7 100644--- a/docs/docs/integrations/mqtt.md+++ b/docs/docs/integrations/mqtt.md@@ -341,3 +341,19 @@ the camera to be removed from the view._### `frigate/<camera_name>/birdseye_mode/state`Topic with current state of the Birdseye mode for a camera. Published values are `CONTINUOUS`, `MOTION`, `OBJECTS`.++### `frigate/<camera_name>/notifications/set`++Topic to turn notifications on and off. Expected values are `ON` and `OFF`.++### `frigate/<camera_name>/notifications/state`++Topic with current state of notifications. Published values are `ON` and `OFF`.++### `frigate/<camera_name>/notifications/suspend`++Topic to suspend notifications for a certain number of minutes. Expected value is an integer.++### `frigate/<camera_name>/notifications/suspended`++Topic with timestamp that notifications are suspended until. Published value is a UNIX timestamp, or 0 if notifications are not suspended.diff --git a/frigate/app.py b/frigate/app.pyindex 1aecce2c35..6ff4a1a412 100644--- a/frigate/app.py+++ b/frigate/app.py@@ -17,8 +17,9 @@ import frigate.util as utilfrom frigate.api.auth import hash_passwordfrom frigate.api.fastapi_app import create_fastapi_appfrom frigate.camera import CameraMetrics, PTZMetrics+from frigate.comms.base_communicator import Communicatorfrom frigate.comms.config_updater import ConfigPublisher-from frigate.comms.dispatcher import Communicator, Dispatcher+from frigate.comms.dispatcher import Dispatcherfrom frigate.comms.event_metadata_updater import (EventMetadataPublisher,EventMetadataTypeEnum,@@ -314,8 +315,14 @@ class FrigateApp:if self.config.mqtt.enabled:comms.append(MqttClient(self.config))- if self.config.notifications.enabled_in_config:- comms.append(WebPushClient(self.config))+ notification_cameras = [+ c+ for c in self.config.cameras.values()+ if c.enabled and c.notifications.enabled_in_config+ ]++ if notification_cameras:+ comms.append(WebPushClient(self.config, self.stop_event))comms.append(WebSocketClient(self.config))comms.append(self.inter_process_communicator)diff --git a/frigate/comms/base_communicator.py b/frigate/comms/base_communicator.pynew file mode 100644index 0000000000..5dfbf1115e--- /dev/null+++ b/frigate/comms/base_communicator.py@@ -0,0 +1,21 @@+from abc import ABC, abstractmethod+from typing import Any, Callable+++class Communicator(ABC):+ """pub/sub model via specific protocol."""++ @abstractmethod+ def publish(self, topic: str, payload: Any, retain: bool = False) -> None:+ """Send data via specific protocol."""+ pass++ @abstractmethod+ def subscribe(self, receiver: Callable) -> None:+ """Pass receiver so communicators can pass commands."""+ pass++ @abstractmethod+ def stop(self) -> None:+ """Stop the communicator."""+ passdiff --git a/frigate/comms/dispatcher.py b/frigate/comms/dispatcher.pyindex 445147df85..e0c2d96e3a 100644--- a/frigate/comms/dispatcher.py+++ b/frigate/comms/dispatcher.py@@ -3,17 +3,19 @@import datetimeimport jsonimport logging-from abc import ABC, abstractmethodfrom typing import Any, Callable, Optionalfrom frigate.camera import PTZMetricsfrom frigate.camera.activity_manager import CameraActivityManager+from frigate.comms.base_communicator import Communicatorfrom frigate.comms.config_updater import ConfigPublisher+from frigate.comms.webpush import WebPushClientfrom frigate.config import BirdseyeModeEnum, FrigateConfigfrom frigate.const import (CLEAR_ONGOING_REVIEW_SEGMENTS,INSERT_MANY_RECORDINGS,INSERT_PREVIEW,+ NOTIFICATION_TEST,REQUEST_REGION_GRID,UPDATE_CAMERA_ACTIVITY,UPDATE_EMBEDDINGS_REINDEX_PROGRESS,@@ -30,25 +32,6 @@ from frigate.util.services import restart_frigatelogger = logging.getLogger(__name__)-class Communicator(ABC):- """pub/sub model via specific protocol."""-- @abstractmethod- def publish(self, topic: str, payload: Any, retain: bool = False) -> None:- """Send data via specific protocol."""- pass-- @abstractmethod- def subscribe(self, receiver: Callable) -> None:- """Pass receiver so communicators can pass commands."""- pass-- @abstractmethod- def stop(self) -> None:- """Stop the communicator."""- pass--class Dispatcher:"""Handle communication between Frigate and communicators."""@@ -77,18 +60,23 @@ class Dispatcher:"motion": self._on_motion_command,"motion_contour_area": self._on_motion_contour_area_command,"motion_threshold": self._on_motion_threshold_command,+ "notifications": self._on_camera_notification_command,"recordings": self._on_recordings_command,"snapshots": self._on_snapshots_command,"birdseye": self._on_birdseye_command,"birdseye_mode": self._on_birdseye_mode_command,}self._global_settings_handlers: dict[str, Callable] = {- "notifications": self._on_notification_command,+ "notifications": self._on_global_notification_command,}for comm in self.comms:comm.subscribe(self._receive)+ self.web_push_client = next(+ (comm for comm in communicators if isinstance(comm, WebPushClient)), None+ )+def _receive(self, topic: str, payload: str) -> Optional[Any]:"""Handle receiving of payload from communicators."""@@ -180,6 +168,13 @@ class Dispatcher:"snapshots": self.config.cameras[camera].snapshots.enabled,"record": self.config.cameras[camera].record.enabled,"audio": self.config.cameras[camera].audio.enabled,+ "notifications": self.config.cameras[camera].notifications.enabled,+ "notifications_suspended": int(+ self.web_push_client.suspended_cameras.get(camera, 0)+ )+ if self.web_push_client+ and camera in self.web_push_client.suspended_cameras+ else 0,"autotracking": self.config.cameras[camera].onvif.autotracking.enabled,@@ -192,6 +187,9 @@ class Dispatcher:json.dumps(self.embeddings_reindex.copy()),)+ def handle_notification_test():+ self.publish("notification_test", "Test notification")+# Dictionary mapping topic to handlerstopic_handlers = {INSERT_MANY_RECORDINGS: handle_insert_many_recordings,@@ -203,13 +201,14 @@ class Dispatcher:UPDATE_EVENT_DESCRIPTION: handle_update_event_description,UPDATE_MODEL_STATE: handle_update_model_state,UPDATE_EMBEDDINGS_REINDEX_PROGRESS: handle_update_embeddings_reindex_progress,+ NOTIFICATION_TEST: handle_notification_test,"restart": handle_restart,"embeddingsReindexProgress": handle_embeddings_reindex_progress,"modelState": handle_model_state,"onConnect": handle_on_connect,}- if topic.endswith("set") or topic.endswith("ptz"):+ if topic.endswith("set") or topic.endswith("ptz") or topic.endswith("suspend"):try:parts = topic.split("/")if len(parts) == 3 and topic.endswith("set"):@@ -224,6 +223,11 @@ class Dispatcher:# example /cam_name/ptz payload=MOVE_UP|MOVE_DOWN|STOP...camera_name = parts[-2]handle_camera_command("ptz", camera_name, "", payload)+ elif len(parts) == 3 and topic.endswith("suspend"):+ # example /cam_name/notifications/suspend payload=duration+ camera_name = parts[-3]+ command = parts[-2]+ self._on_camera_notification_suspend(camera_name, payload)except IndexError:logger.error(f"Received invalid {topic.split('/')[-1]} command: {topic}"@@ -365,16 +369,18 @@ class Dispatcher:self.config_updater.publish(f"config/motion/{camera_name}", motion_settings)self.publish(f"{camera_name}/motion_threshold/state", payload, retain=True)- def _on_notification_command(self, payload: str) -> None:- """Callback for notification topic."""+ def _on_global_notification_command(self, payload: str) -> None:+ """Callback for global notification topic."""if payload != "ON" and payload != "OFF":- f"Received unsupported value for notification: {payload}"+ f"Received unsupported value for all notification: {payload}"returnnotification_settings = self.config.notifications- logger.info(f"Setting notifications: {payload}")+ logger.info(f"Setting all notifications: {payload}")notification_settings.enabled = payload == "ON" # type: ignore[union-attr]- self.config_updater.publish("config/notifications", notification_settings)+ self.config_updater.publish(+ "config/notifications", {"_global_notifications": notification_settings}+ )self.publish("notifications/state", payload, retain=True)def _on_audio_command(self, camera_name: str, payload: str) -> None:@@ -491,3 +497,71 @@ class Dispatcher:self.config_updater.publish(f"config/birdseye/{camera_name}", birdseye_settings)self.publish(f"{camera_name}/birdseye_mode/state", payload, retain=True)++ def _on_camera_notification_command(self, camera_name: str, payload: str) -> None:+ """Callback for camera level notifications topic."""+ notification_settings = self.config.cameras[camera_name].notifications++ if payload == "ON":+ if not self.config.cameras[camera_name].notifications.enabled_in_config:+ logger.error(+ "Notifications must be enabled in the config to be turned on via MQTT."+ )+ return++ if not notification_settings.enabled:+ logger.info(f"Turning on notifications for {camera_name}")+ notification_settings.enabled = True+ if (+ self.web_push_client+ and camera_name in self.web_push_client.suspended_cameras+ ):+ self.web_push_client.suspended_cameras[camera_name] = 0+ elif payload == "OFF":+ if notification_settings.enabled:+ logger.info(f"Turning off notifications for {camera_name}")+ notification_settings.enabled = False+ if (+ self.web_push_client+ and camera_name in self.web_push_client.suspended_cameras+ ):+ self.web_push_client.suspended_cameras[camera_name] = 0++ self.config_updater.publish(+ "config/notifications", {camera_name: notification_settings}+ )+ self.publish(f"{camera_name}/notifications/state", payload, retain=True)+ self.publish(f"{camera_name}/notifications/suspended", "0", retain=True)++ def _on_camera_notification_suspend(self, camera_name: str, payload: str) -> None:+ """Callback for camera level notifications suspend topic."""+ try:+ duration = int(payload)+ except ValueError:+ logger.error(f"Invalid suspension duration: {payload}")+ return++ if self.web_push_client is None:+ logger.error("WebPushClient not available for suspension")+ return++ notification_settings = self.config.cameras[camera_name].notifications++ if not notification_settings.enabled:+ logger.error(f"Notifications are not enabled for {camera_name}")+ return++ if duration != 0:+ self.web_push_client.suspend_notifications(camera_name, duration)+ else:+ self.web_push_client.unsuspend_notifications(camera_name)++ self.publish(+ f"{camera_name}/notifications/suspended",+ str(+ int(self.web_push_client.suspended_cameras.get(camera_name, 0))+ if camera_name in self.web_push_client.suspended_cameras+ else 0+ ),+ retain=True,+ )diff --git a/frigate/comms/inter_process.py b/frigate/comms/inter_process.pyindex 850e2435c9..36a6857a4a 100644--- a/frigate/comms/inter_process.py+++ b/frigate/comms/inter_process.py@@ -7,7 +7,7 @@ from typing import Callableimport zmq-from frigate.comms.dispatcher import Communicator+from frigate.comms.base_communicator import CommunicatorSOCKET_REP_REQ = "ipc:///tmp/cache/comms"diff --git a/frigate/comms/mqtt.py b/frigate/comms/mqtt.pyindex 33478d5c4e..57460b29ba 100644--- a/frigate/comms/mqtt.py+++ b/frigate/comms/mqtt.py@@ -5,7 +5,7 @@ from typing import Any, Callableimport paho.mqtt.client as mqttfrom paho.mqtt.enums import CallbackAPIVersion-from frigate.comms.dispatcher import Communicator+from frigate.comms.base_communicator import Communicatorfrom frigate.config import FrigateConfiglogger = logging.getLogger(__name__)diff --git a/frigate/comms/webpush.py b/frigate/comms/webpush.pyindex abfd52d198..b96b6f3c0f 100644--- a/frigate/comms/webpush.py+++ b/frigate/comms/webpush.py@@ -4,13 +4,17 @@ import datetimeimport jsonimport loggingimport os+import queue+import threading+from dataclasses import dataclass+from multiprocessing.synchronize import Event as MpEventfrom typing import Any, Callablefrom py_vapid import Vapid01from pywebpush import WebPusher+from frigate.comms.base_communicator import Communicatorfrom frigate.comms.config_updater import ConfigSubscriber-from frigate.comms.dispatcher import Communicatorfrom frigate.config import FrigateConfigfrom frigate.const import CONFIG_DIRfrom frigate.models import User@@ -18,15 +22,36 @@ from frigate.models import Userlogger = logging.getLogger(__name__)+@dataclass+class PushNotification:+ user: str+ payload: dict[str, Any]+ title: str+ message: str+ direct_url: str = ""+ image: str = ""+ notification_type: str = "alert"+ ttl: int = 0++class WebPushClient(Communicator): # type: ignore[misc]"""Frigate wrapper for webpush client."""- def __init__(self, config: FrigateConfig) -> None:+ def __init__(self, config: FrigateConfig, stop_event: MpEvent) -> None:self.config = configself.claim_headers: dict[str, dict[str, str]] = {}self.refresh: int = 0self.web_pushers: dict[str, list[WebPusher]] = {}self.expired_subs: dict[str, list[str]] = {}+ self.suspended_cameras: dict[str, int] = {+ c.name: 0 for c in self.config.cameras.values()+ }+ self.notification_queue: queue.Queue[PushNotification] = queue.Queue()+ self.notification_thread = threading.Thread(+ target=self._process_notifications, daemon=True+ )+ self.notification_thread.start()+ self.stop_event = stop_eventif not self.config.notifications.email:logger.warning("Email must be provided for push notifications to be sent.")@@ -103,30 +128,144 @@ class WebPushClient(Communicator): # type: ignore[misc]self.expired_subs = {}+ def suspend_notifications(self, camera: str, minutes: int) -> None:+ """Suspend notifications for a specific camera."""+ suspend_until = int(+ (datetime.datetime.now() + datetime.timedelta(minutes=minutes)).timestamp()+ )+ self.suspended_cameras[camera] = suspend_until+ logger.info(+ f"Notifications for {camera} suspended until {datetime.datetime.fromtimestamp(suspend_until).strftime('%Y-%m-%d %H:%M:%S')}"+ )++ def unsuspend_notifications(self, camera: str) -> None:+ """Unsuspend notifications for a specific camera."""+ self.suspended_cameras[camera] = 0+ logger.info(f"Notifications for {camera} unsuspended")++ def is_camera_suspended(self, camera: str) -> bool:+ return datetime.datetime.now().timestamp() <= self.suspended_cameras[camera]+def publish(self, topic: str, payload: Any, retain: bool = False) -> None:"""Wrapper for publishing when client is in valid state."""# check for updated notification config_, updated_notification_config = self.config_subscriber.check_for_update()if updated_notification_config:- self.config.notifications = updated_notification_config+ for key, value in updated_notification_config.items():+ if key == "_global_notifications":+ self.config.notifications = value- if not self.config.notifications.enabled:- return+ elif key in self.config.cameras:+ self.config.cameras[key].notifications = valueif topic == "reviews":- self.send_alert(json.loads(payload))+ decoded = json.loads(payload)+ camera = decoded["before"]["camera"]+ if not self.config.cameras[camera].notifications.enabled:+ return+ if self.is_camera_suspended(camera):+ logger.debug(f"Notifications for {camera} are currently suspended.")+ return+ self.send_alert(decoded)+ elif topic == "notification_test":+ if not self.config.notifications.enabled:+ return+ self.send_notification_test()++ def send_push_notification(+ self,+ user: str,+ payload: dict[str, Any],+ title: str,+ message: str,+ direct_url: str = "",+ image: str = "",+ notification_type: str = "alert",+ ttl: int = 0,+ ) -> None:+ notification = PushNotification(+ user=user,+ payload=payload,+ title=title,+ message=message,+ direct_url=direct_url,+ image=image,+ notification_type=notification_type,+ ttl=ttl,+ )+ self.notification_queue.put(notification)++ def _process_notifications(self) -> None:+ while not self.stop_event.is_set():+ try:+ notification = self.notification_queue.get(timeout=1.0)+ self.check_registrations()++ for pusher in self.web_pushers[notification.user]:+ endpoint = pusher.subscription_info["endpoint"]+ headers = self.claim_headers[+ endpoint[: endpoint.index("/", 10)]+ ].copy()+ headers["urgency"] = "high"++ resp = pusher.send(+ headers=headers,+ ttl=notification.ttl,+ data=json.dumps(+ {+ "title": notification.title,+ "message": notification.message,+ "direct_url": notification.direct_url,+ "image": notification.image,+ "id": notification.payload.get("after", {}).get(+ "id", ""+ ),+ "type": notification.notification_type,+ }+ ),+ timeout=10,+ )- def send_alert(self, payload: dict[str, any]) -> None:+ if resp.status_code in (404, 410):+ self.expired_subs.setdefault(notification.user, []).append(+ endpoint+ )+ elif resp.status_code != 201:+ logger.warning(+ f"Failed to send notification to {notification.user} :: {resp.status_code}"+ )++ except queue.Empty:+ continue+ except Exception as e:+ logger.error(f"Error processing notification: {str(e)}")++ def send_notification_test(self) -> None:if not self.config.notifications.email:returnself.check_registrations()- # Only notify for alerts- if payload["after"]["severity"] != "alert":+ for user in self.web_pushers:+ self.send_push_notification(+ user=user,+ payload={},+ title="Test Notification",+ message="This is a test notification from Frigate.",+ direct_url="/",+ notification_type="test",+ )++ def send_alert(self, payload: dict[str, Any]) -> None:+ if (+ not self.config.notifications.email+ or payload["after"]["severity"] != "alert"+ ):return+ self.check_registrations()+state = payload["type"]# Don't notify if message is an update and important fields don't have an update@@ -155,49 +294,21 @@ class WebPushClient(Communicator): # type: ignore[misc]# if event is ongoing open to live view otherwise open to recordings viewdirect_url = f"/review?id={reviewId}" if state == "end" else f"/#{camera}"-- for user, pushers in self.web_pushers.items():- for pusher in pushers:- endpoint = pusher.subscription_info["endpoint"]-- # set headers for notification behavior- headers = self.claim_headers[- endpoint[0 : endpoint.index("/", 10)]- ].copy()- headers["urgency"] = "high"- ttl = 3600 if state == "end" else 0-- # send message- resp = pusher.send(- headers=headers,- ttl=ttl,- data=json.dumps(- {- "title": title,- "message": message,- "direct_url": direct_url,- "image": image,- "id": reviewId,- "type": "alert",- }- ),- )-- if resp.status_code == 201:- pass- elif resp.status_code == 404 or resp.status_code == 410:- # subscription is not found or has been unsubscribed- if not self.expired_subs.get(user):- self.expired_subs[user] = []-- self.expired_subs[user].append(pusher.subscription_info["endpoint"])- # the subscription no longer exists and should be removed- else:- logger.warning(- f"Failed to send notification to {user} :: {resp.headers}"- )+ ttl = 3600 if state == "end" else 0++ for user in self.web_pushers:+ self.send_push_notification(+ user=user,+ payload=payload,+ title=title,+ message=message,+ direct_url=direct_url,+ image=image,+ ttl=ttl,+ )self.cleanup_registrations()def stop(self) -> None:- pass+ logger.info("Closing notification queue")+ self.notification_thread.join()diff --git a/frigate/comms/ws.py b/frigate/comms/ws.pyindex fccd8db5c3..1eed290f77 100644--- a/frigate/comms/ws.py+++ b/frigate/comms/ws.py@@ -15,7 +15,7 @@ from ws4py.server.wsgirefserver import (from ws4py.server.wsgiutils import WebSocketWSGIApplicationfrom ws4py.websocket import WebSocket as WebSocket_-from frigate.comms.dispatcher import Communicator+from frigate.comms.base_communicator import Communicatorfrom frigate.config import FrigateConfiglogger = logging.getLogger(__name__)diff --git a/frigate/config/__init__.py b/frigate/config/__init__.pyindex e90c336e51..c6ff535b05 100644--- a/frigate/config/__init__.py+++ b/frigate/config/__init__.py@@ -8,7 +8,6 @@ from .config import * # noqa: F403from .database import * # noqa: F403from .logger import * # noqa: F403from .mqtt import * # noqa: F403-from .notification import * # noqa: F403from .proxy import * # noqa: F403from .telemetry import * # noqa: F403from .tls import * # noqa: F403diff --git a/frigate/config/camera/camera.py b/frigate/config/camera/camera.pyindex 69fa1b4550..50f61f33ca 100644--- a/frigate/config/camera/camera.py+++ b/frigate/config/camera/camera.py@@ -25,6 +25,7 @@ from .genai import GenAICameraConfigfrom .live import CameraLiveConfigfrom .motion import MotionConfigfrom .mqtt import CameraMqttConfig+from .notification import NotificationConfigfrom .objects import ObjectConfigfrom .onvif import OnvifConfigfrom .record import RecordConfig@@ -85,6 +86,9 @@ class CameraConfig(FrigateBaseModel):mqtt: CameraMqttConfig = Field(default_factory=CameraMqttConfig, title="MQTT configuration.")+ notifications: NotificationConfig = Field(+ default_factory=NotificationConfig, title="Notifications configuration."+ )onvif: OnvifConfig = Field(default_factory=OnvifConfig, title="Camera Onvif Configuration.")diff --git a/frigate/config/notification.py b/frigate/config/camera/notification.pysimilarity index 92%rename from frigate/config/notification.pyrename to frigate/config/camera/notification.pyindex 0ffebff3c4..79355b8aea 100644--- a/frigate/config/notification.py+++ b/frigate/config/camera/notification.py@@ -2,7 +2,7 @@ from typing import Optionalfrom pydantic import Field-from .base import FrigateBaseModel+from ..base import FrigateBaseModel__all__ = ["NotificationConfig"]diff --git a/frigate/config/config.py b/frigate/config/config.pyindex 694a3389f5..aea41a7bcb 100644--- a/frigate/config/config.py+++ b/frigate/config/config.py@@ -46,6 +46,7 @@ from .camera.detect import DetectConfigfrom .camera.ffmpeg import FfmpegConfigfrom .camera.genai import GenAIConfigfrom .camera.motion import MotionConfig+from .camera.notification import NotificationConfigfrom .camera.objects import FilterConfig, ObjectConfigfrom .camera.record import RecordConfig, RetainModeEnumfrom .camera.review import ReviewConfig@@ -62,7 +63,6 @@ from .database import DatabaseConfigfrom .env import EnvVarsfrom .logger import LoggerConfigfrom .mqtt import MqttConfig-from .notification import NotificationConfigfrom .proxy import ProxyConfigfrom .telemetry import TelemetryConfigfrom .tls import TlsConfig@@ -332,7 +332,7 @@ class FrigateConfig(FrigateBaseModel):)mqtt: MqttConfig = Field(title="MQTT configuration.")notifications: NotificationConfig = Field(- default_factory=NotificationConfig, title="Notification configuration."+ default_factory=NotificationConfig, title="Global notification configuration.")proxy: ProxyConfig = Field(default_factory=ProxyConfig, title="Proxy configuration."@@ -452,6 +452,7 @@ class FrigateConfig(FrigateBaseModel):"review": ...,"genai": ...,"motion": ...,+ "notifications": ...,"detect": ...,"ffmpeg": ...,"timestamp_style": ...,@@ -527,6 +528,9 @@ class FrigateConfig(FrigateBaseModel):# set config pre-valuecamera_config.audio.enabled_in_config = camera_config.audio.enabledcamera_config.record.enabled_in_config = camera_config.record.enabled+ camera_config.notifications.enabled_in_config = (+ camera_config.notifications.enabled+ )camera_config.onvif.autotracking.enabled_in_config = (camera_config.onvif.autotracking.enabled)diff --git a/frigate/const.py b/frigate/const.pyindex 559d7552f6..16df8b887e 100644--- a/frigate/const.py+++ b/frigate/const.py@@ -104,6 +104,7 @@ UPDATE_CAMERA_ACTIVITY = "update_camera_activity"UPDATE_EVENT_DESCRIPTION = "update_event_description"UPDATE_MODEL_STATE = "update_model_state"UPDATE_EMBEDDINGS_REINDEX_PROGRESS = "handle_embeddings_reindex_progress"+NOTIFICATION_TEST = "notification_test"# Stats Valuesdiff --git a/web/src/api/ws.tsx b/web/src/api/ws.tsxindex 9b8924d1b0..3ac7c9fee7 100644--- a/web/src/api/ws.tsx+++ b/web/src/api/ws.tsx@@ -53,13 +53,26 @@ function useValue(): useValueReturn {const cameraStates: WsState = {};Object.entries(cameraActivity).forEach(([name, state]) => {- const { record, detect, snapshots, audio, autotracking } =+ const {+ record,+ detect,+ snapshots,+ audio,+ notifications,+ notifications_suspended,+ autotracking,+ } =// @ts-expect-error we know this is correctstate["config"];cameraStates[`${name}/recordings/state`] = record ? "ON" : "OFF";cameraStates[`${name}/detect/state`] = detect ? "ON" : "OFF";cameraStates[`${name}/snapshots/state`] = snapshots ? "ON" : "OFF";cameraStates[`${name}/audio/state`] = audio ? "ON" : "OFF";+ cameraStates[`${name}/notifications/state`] = notifications+ ? "ON"+ : "OFF";+ cameraStates[`${name}/notifications/suspended`] =+ notifications_suspended || 0;cameraStates[`${name}/ptz_autotracker/state`] = autotracking? "ON": "OFF";@@ -413,3 +426,39 @@ export function useTrackedObjectUpdate(): { payload: string } {} = useWs("tracked_object_update", "");return useDeepMemo(JSON.parse(payload as string));}++export function useNotifications(camera: string): {+ payload: ToggleableSetting;+ send: (payload: string, retain?: boolean) => void;+} {+ const {+ value: { payload },+ send,+ } = useWs(`${camera}/notifications/state`, `${camera}/notifications/set`);+ return { payload: payload as ToggleableSetting, send };+}++export function useNotificationSuspend(camera: string): {+ payload: string;+ send: (payload: number, retain?: boolean) => void;+} {+ const {+ value: { payload },+ send,+ } = useWs(+ `${camera}/notifications/suspended`,+ `${camera}/notifications/suspend`,+ );+ return { payload: payload as string, send };+}++export function useNotificationTest(): {+ payload: string;+ send: (payload: string, retain?: boolean) => void;+} {+ const {+ value: { payload },+ send,+ } = useWs("notification_test", "notification_test");+ return { payload: payload as string, send };+}diff --git a/web/src/types/frigateConfig.ts b/web/src/types/frigateConfig.tsindex 794683adfe..073308d58a 100644--- a/web/src/types/frigateConfig.ts+++ b/web/src/types/frigateConfig.ts@@ -111,6 +111,11 @@ export interface CameraConfig {timestamp: boolean;};name: string;+ notifications: {+ enabled: boolean;+ email?: string;+ enabled_in_config: boolean;+ };objects: {filters: {[objectName: string]: {@@ -393,6 +398,7 @@ export interface FrigateConfig {notifications: {enabled: boolean;email?: string;+ enabled_in_config: boolean;};objects: {diff --git a/web/src/views/settings/NotificationsSettingsView.tsx b/web/src/views/settings/NotificationsSettingsView.tsxindex 7518172459..5ea545fb33 100644--- a/web/src/views/settings/NotificationsSettingsView.tsx+++ b/web/src/views/settings/NotificationsSettingsView.tsx@@ -14,24 +14,38 @@ import { Input } from "@/components/ui/input";import { Label } from "@/components/ui/label";import { Separator } from "@/components/ui/separator";import { Toaster } from "@/components/ui/sonner";-import { Switch } from "@/components/ui/switch";import { StatusBarMessagesContext } from "@/context/statusbar-provider";import { FrigateConfig } from "@/types/frigateConfig";import { zodResolver } from "@hookform/resolvers/zod";import axios from "axios";-import { useCallback, useContext, useEffect, useState } from "react";+import { useCallback, useContext, useEffect, useMemo, useState } from "react";import { useForm } from "react-hook-form";-import { LuExternalLink } from "react-icons/lu";+import { LuCheck, LuExternalLink, LuX } from "react-icons/lu";import { Link } from "react-router-dom";import { toast } from "sonner";import useSWR from "swr";import { z } from "zod";+import {+ useNotifications,+ useNotificationSuspend,+ useNotificationTest,+} from "@/api/ws";+import {+ Select,+ SelectTrigger,+ SelectValue,+ SelectContent,+ SelectItem,+} from "@/components/ui/select";+import { formatUnixTimestampToDateTime } from "@/utils/dateUtil";+import FilterSwitch from "@/components/filter/FilterSwitch";const NOTIFICATION_SERVICE_WORKER = "notifications-worker.js";type NotificationSettingsValueType = {- enabled: boolean;+ allEnabled: boolean;email?: string;+ cameras: string[];};type NotificationsSettingsViewProps = {@@ -47,9 +61,52 @@ export default function NotificationView({},);+ const allCameras = useMemo(() => {+ if (!config) {+ return [];+ }++ return Object.values(config.cameras).sort(+ (aConf, bConf) => aConf.ui.order - bConf.ui.order,+ );+ }, [config]);++ const notificationCameras = useMemo(() => {+ if (!config) {+ return [];+ }++ return Object.values(config.cameras)+ .filter(+ (conf) =>+ conf.enabled &&+ conf.notifications &&+ conf.notifications.enabled_in_config,+ )+ .sort((aConf, bConf) => aConf.ui.order - bConf.ui.order);+ }, [config]);++ const { send: sendTestNotification } = useNotificationTest();+// status barconst { addMessage, removeMessage } = useContext(StatusBarMessagesContext)!;+ const [changedValue, setChangedValue] = useState(false);++ useEffect(() => {+ if (changedValue) {+ addMessage(+ "notification_settings",+ `Unsaved notification settings`,+ undefined,+ `notification_settings`,+ );+ } else {+ removeMessage("notification_settings", `notification_settings`);+ }+ // we know that these deps are correct+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [changedValue]);// notification key handling@@ -87,7 +144,7 @@ export default function NotificationView({setRegistration(null);});toast.success(- "Successfully registered for notifications. Restart to start receiving notifications.",+ "Successfully registered for notifications. Restarting Frigate is required before any notifications (including a test notification) can be sent.",{position: "top-center",},@@ -122,28 +179,44 @@ export default function NotificationView({const [isLoading, setIsLoading] = useState(false);const formSchema = z.object({- enabled: z.boolean(),+ allEnabled: z.boolean(),email: z.string(),+ cameras: z.array(z.string()),});const form = useForm<z.infer<typeof formSchema>>({resolver: zodResolver(formSchema),mode: "onChange",defaultValues: {- enabled: config?.notifications.enabled,+ allEnabled: config?.notifications.enabled,email: config?.notifications.email,+ cameras: config?.notifications.enabled+ ? []+ : notificationCameras.map((c) => c.name),},});+ const watchCameras = form.watch("cameras");++ useEffect(() => {+ if (watchCameras.length > 0) {+ form.setValue("allEnabled", false);+ }+ }, [watchCameras, allCameras, form]);+const onCancel = useCallback(() => {if (!config) {return;}setUnsavedChanges(false);+ setChangedValue(false);form.reset({- enabled: config.notifications.enabled,+ allEnabled: config.notifications.enabled,email: config.notifications.email || "",+ cameras: config?.notifications.enabled+ ? []+ : notificationCameras.map((c) => c.name),});// we know that these deps are correct// eslint-disable-next-line react-hooks/exhaustive-deps@@ -151,11 +224,27 @@ export default function NotificationView({const saveToConfig = useCallback(async (- { enabled, email }: NotificationSettingsValueType, // values submitted via the form+ { allEnabled, email, cameras }: NotificationSettingsValueType, // values submitted via the form) => {+ const allCameraNames = allCameras.map((cam) => cam.name);++ const enabledCameraQueries = cameras+ .map((cam) => `&cameras.${cam}.notifications.enabled=True`)+ .join("");++ const disabledCameraQueries = allCameraNames+ .filter((cam) => !cameras.includes(cam))+ .map(+ (cam) =>+ `&cameras.${cam}.notifications.enabled=${allEnabled ? "True" : "False"}`,+ )+ .join("");++ const allCameraQueries = enabledCameraQueries + disabledCameraQueries;+axios.put(- `config/set?notifications.enabled=${enabled}¬ifications.email=${email}`,+ `config/set?notifications.enabled=${allEnabled ? "True" : "False"}¬ifications.email=${email}${allCameraQueries}`,{requires_restart: 0,},@@ -182,7 +271,7 @@ export default function NotificationView({setIsLoading(false);});},- [updateConfig, setIsLoading],+ [updateConfig, setIsLoading, allCameras],);function onSubmit(values: z.infer<typeof formSchema>) {@@ -195,149 +284,249 @@ export default function NotificationView({<div className="flex size-full flex-col md:flex-row"><Toaster position="top-center" closeButton={true} /><div className="scrollbar-container order-last mb-10 mt-2 flex h-full w-full flex-col overflow-y-auto rounded-lg border-[1px] border-secondary-foreground bg-background_alt p-2 md:order-none md:mb-0 md:mr-2 md:mt-0">- <Heading as="h3" className="my-2">- Notification Settings- </Heading>-- <div className="max-w-6xl">- <div className="mb-5 mt-2 flex max-w-5xl flex-col gap-2 text-sm text-primary-variant">- <p>- Frigate can natively send push notifications to your device when- it is running in the browser or installed as a PWA.- </p>- <div className="flex items-center text-primary">- <Link- to="https://docs.frigate.video/configuration/notifications"- target="_blank"- rel="noopener noreferrer"- className="inline"- >- Read the Documentation{" "}- <LuExternalLink className="ml-2 inline-flex size-3" />- </Link>+ <div className="grid w-full grid-cols-1 gap-4 md:grid-cols-2">+ <div className="col-span-1">+ <Heading as="h3" className="my-2">+ Notification Settings+ </Heading>++ <div className="max-w-6xl">+ <div className="mb-5 mt-2 flex max-w-5xl flex-col gap-2 text-sm text-primary-variant">+ <p>+ Frigate can natively send push notifications to your device+ when it is running in the browser or installed as a PWA.+ </p>+ <div className="flex items-center text-primary">+ <Link+ to="https://docs.frigate.video/configuration/notifications"+ target="_blank"+ rel="noopener noreferrer"+ className="inline"+ >+ Read the Documentation{" "}+ <LuExternalLink className="ml-2 inline-flex size-3" />+ </Link>+ </div>+ </div></div>- </div>- </div>- <Form {...form}>- <form- onSubmit={form.handleSubmit(onSubmit)}- className="mt-2 space-y-6"- >- <FormField- control={form.control}- name="enabled"- render={({ field }) => (- <FormItem>- <FormControl>- <div className="flex flex-row items-center justify-start gap-2">- <Label className="cursor-pointer" htmlFor="auto-live">- Notifications- </Label>- <Switch- id="auto-live"- checked={field.value}- onCheckedChange={(checked) => {- return field.onChange(checked);- }}- />- </div>- </FormControl>- </FormItem>- )}- />- <FormField- control={form.control}- name="email"- render={({ field }) => (- <FormItem>- <FormLabel>Email</FormLabel>- <FormControl>- <Input- className="text-md w-full border border-input bg-background p-2 hover:bg-accent hover:text-accent-foreground dark:[color-scheme:dark] md:w-72"- placeholder="[email protected]"- {...field}- />- </FormControl>- <FormDescription>- Entering a valid email is required, as this is used by the- push server in case problems occur.- </FormDescription>- <FormMessage />- </FormItem>- )}- />- <div className="flex w-full flex-row items-center gap-2 pt-2 md:w-[25%]">- <Button- className="flex flex-1"- aria-label="Cancel"- onClick={onCancel}- type="button"- >- Cancel- </Button>- <Button- variant="select"- disabled={isLoading}- className="flex flex-1"- aria-label="Save"- type="submit"+ <Form {...form}>+ <form+ onSubmit={form.handleSubmit(onSubmit)}+ className="mt-2 space-y-6">- {isLoading ? (- <div className="flex flex-row items-center gap-2">- <ActivityIndicator />- <span>Saving...</span>- </div>- ) : (- "Save"- )}- </Button>- </div>- </form>- </Form>-- <div className="mt-4 space-y-6">- <div className="space-y-3">- <Separator className="my-2 flex bg-secondary" />- <Button- aria-label="Register or unregister notifications for this device"- disabled={- !config?.notifications.enabled || publicKey == undefined- }- onClick={() => {- if (registration == null) {- Notification.requestPermission().then((permission) => {- if (permission === "granted") {- navigator.serviceWorker- .register(NOTIFICATION_SERVICE_WORKER)- .then((registration) => {- setRegistration(registration);-- if (registration.active) {- subscribeToNotifications(registration);- } else {- setTimeout(- () => subscribeToNotifications(registration),- 1000,- );- }+ <FormField+ control={form.control}+ name="email"+ render={({ field }) => (+ <FormItem>+ <FormLabel>Email</FormLabel>+ <FormControl>+ <Input+ className="text-md w-full border border-input bg-background p-2 hover:bg-accent hover:text-accent-foreground dark:[color-scheme:dark] md:w-72"+ placeholder="[email protected]"+ {...field}+ />+ </FormControl>+ <FormDescription>+ Entering a valid email is required, as this is used by+ the push server in case problems occur.+ </FormDescription>+ <FormMessage />+ </FormItem>+ )}+ />++ <FormField+ control={form.control}+ name="cameras"+ render={({ field }) => (+ <FormItem>+ {allCameras && allCameras?.length > 0 ? (+ <>+ <div className="mb-2">+ <FormLabel className="flex flex-row items-center text-base">+ Cameras+ </FormLabel>+ </div>+ <div className="max-w-md space-y-2 rounded-lg bg-secondary p-4">+ <FormField+ control={form.control}+ name="allEnabled"+ render={({ field }) => (+ <FilterSwitch+ label="All Cameras"+ isChecked={field.value}+ onCheckedChange={(checked) => {+ setChangedValue(true);+ if (checked) {+ form.setValue("cameras", []);+ }+ field.onChange(checked);+ }}+ />+ )}+ />+ {allCameras?.map((camera) => (+ <FilterSwitch+ key={camera.name}+ label={camera.name.replaceAll("_", " ")}+ isChecked={field.value?.includes(camera.name)}+ onCheckedChange={(checked) => {+ setChangedValue(true);+ let newCameras;+ if (checked) {+ newCameras = [+ ...field.value,+ camera.name,+ ];+ } else {+ newCameras = field.value?.filter(+ (value) => value !== camera.name,+ );+ }+ field.onChange(newCameras);+ form.setValue("allEnabled", false);+ }}+ />+ ))}+ </div>+ </>+ ) : (+ <div className="font-normal text-destructive">+ No cameras available.+ </div>+ )}++ <FormMessage />+ <FormDescription>+ Select the cameras to enable notifications for.+ </FormDescription>+ </FormItem>+ )}+ />++ <div className="flex w-full flex-row items-center gap-2 pt-2 md:w-[50%]">+ <Button+ className="flex flex-1"+ aria-label="Cancel"+ onClick={onCancel}+ type="button"+ >+ Cancel+ </Button>+ <Button+ variant="select"+ disabled={isLoading}+ className="flex flex-1"+ aria-label="Save"+ type="submit"+ >+ {isLoading ? (+ <div className="flex flex-row items-center gap-2">+ <ActivityIndicator />+ <span>Saving...</span>+ </div>+ ) : (+ "Save"+ )}+ </Button>+ </div>+ </form>+ </Form>+ </div>++ <div className="col-span-1">+ <div className="mt-4 gap-2 space-y-6">+ <div className="flex flex-col gap-2 md:max-w-[50%]">+ <Separator className="my-2 flex bg-secondary md:hidden" />+ <Heading as="h4" className="my-2">+ Device-Specific Settings+ </Heading>+ <Button+ aria-label="Register or unregister notifications for this device"+ disabled={+ !config?.notifications.enabled || publicKey == undefined+ }+ onClick={() => {+ if (registration == null) {+ Notification.requestPermission().then((permission) => {+ if (permission === "granted") {+ navigator.serviceWorker+ .register(NOTIFICATION_SERVICE_WORKER)+ .then((registration) => {+ setRegistration(registration);++ if (registration.active) {+ subscribeToNotifications(registration);+ } else {+ setTimeout(+ () =>+ subscribeToNotifications(registration),+ 1000,+ );+ }+ });+ }+ });+ } else {+ registration.pushManager+ .getSubscription()+ .then((pushSubscription) => {+ pushSubscription?.unsubscribe();+ registration.unregister();+ setRegistration(null);+ removeMessage(+ "notification_settings",+ "registration",+ );});}- });- } else {- registration.pushManager- .getSubscription()- .then((pushSubscription) => {- pushSubscription?.unsubscribe();- registration.unregister();- setRegistration(null);- removeMessage("notification_settings", "registration");- });- }- }}- >- {`${registration != null ? "Unregister" : "Register"} for notifications on this device`}- </Button>+ }}+ >+ {`${registration != null ? "Unregister" : "Register"} for notifications on this device`}+ </Button>+ {registration != null && registration.active && (+ <Button+ aria-label="Send a test notification"+ onClick={() => sendTestNotification("notification_test")}+ >+ Send a test notification+ </Button>+ )}+ </div>+ </div>+ {notificationCameras.length > 0 && (+ <div className="mt-4 gap-2 space-y-6">+ <div className="space-y-3">+ <Separator className="my-2 flex bg-secondary" />+ <Heading as="h4" className="my-2">+ Global Settings+ </Heading>+ <div className="max-w-xl">+ <div className="mb-5 mt-2 flex flex-col gap-2 text-sm text-primary-variant">+ <p>+ Temporarily suspend notifications for specific cameras+ on all registered devices.+ </p>+ </div>+ </div>++ <div className="flex max-w-2xl flex-col gap-2.5">+ <div className="rounded-lg bg-secondary p-5">+ <div className="grid gap-6">+ {notificationCameras.map((item) => (+ <CameraNotificationSwitch+ config={config}+ camera={item.name}+ />+ ))}+ </div>+ </div>+ </div>+ </div>+ </div>+ )}</div></div></div>@@ -345,3 +534,110 @@ export default function NotificationView({</>);}++type CameraNotificationSwitchProps = {+ config?: FrigateConfig;+ camera: string;+};++export function CameraNotificationSwitch({+ config,+ camera,+}: CameraNotificationSwitchProps) {+ const { payload: notificationState, send: sendNotification } =+ useNotifications(camera);+ const { payload: notificationSuspendUntil, send: sendNotificationSuspend } =+ useNotificationSuspend(camera);+ const [isSuspended, setIsSuspended] = useState<boolean>(false);++ useEffect(() => {+ if (notificationSuspendUntil) {+ setIsSuspended(+ notificationSuspendUntil !== "0" || notificationState === "OFF",+ );+ }+ }, [notificationSuspendUntil, notificationState]);++ const handleSuspend = (duration: string) => {+ setIsSuspended(true);+ if (duration == "off") {+ sendNotification("OFF");+ } else {+ sendNotificationSuspend(parseInt(duration));+ }+ };++ const handleCancelSuspension = () => {+ sendNotification("ON");+ sendNotificationSuspend(0);+ };++ const formatSuspendedUntil = (timestamp: string) => {+ if (timestamp === "0") return "Frigate restarts.";++ return formatUnixTimestampToDateTime(parseInt(timestamp), {+ time_style: "medium",+ date_style: "medium",+ timezone: config?.ui.timezone,+ strftime_fmt: `%b %d, ${config?.ui.time_format == "24hour" ? "%H:%M" : "%I:%M %p"}`,+ });+ };++ return (+ <div className="flex items-center justify-between gap-2">+ <div className="flex flex-col items-start justify-start">+ <div className="flex flex-row items-center justify-start gap-3">+ {!isSuspended ? (+ <LuCheck className="size-6 text-success" />+ ) : (+ <LuX className="size-6 text-danger" />+ )}+ <div className="flex flex-col">+ <Label+ className="text-md cursor-pointer capitalize text-primary"+ htmlFor="camera"+ >+ {camera.replaceAll("_", " ")}+ </Label>++ {!isSuspended ? (+ <div className="flex flex-row items-center gap-2 text-sm text-success">+ Notifications Active+ </div>+ ) : (+ <div className="flex flex-row items-center gap-2 text-sm text-danger">+ Notifications suspended until{" "}+ {formatSuspendedUntil(notificationSuspendUntil)}+ </div>+ )}+ </div>+ </div>+ </div>++ {!isSuspended ? (+ <Select onValueChange={handleSuspend}>+ <SelectTrigger className="w-auto">+ <SelectValue placeholder="Suspend" />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="5">Suspend for 5 minutes</SelectItem>+ <SelectItem value="10">Suspend for 10 minutes</SelectItem>+ <SelectItem value="30">Suspend for 30 minutes</SelectItem>+ <SelectItem value="60">Suspend for 1 hour</SelectItem>+ <SelectItem value="840">Suspend for 12 hours</SelectItem>+ <SelectItem value="1440">Suspend for 24 hours</SelectItem>+ <SelectItem value="off">Suspend until restart</SelectItem>+ </SelectContent>+ </Select>+ ) : (+ <Button+ variant="destructive"+ size="sm"+ onClick={handleCancelSuspension}+ >+ Cancel Suspension+ </Button>+ )}+ </div>+ );+}