Use dataclasses for config handling

Use config data classes to eliminate some of the boilerplate associated
with setting up the configuration.  In particular, using dataclasses
removes a lot of the boilerplate around assigning properties to the
object and allows these to be easily immutable by freezing them.  In the
case of simple, non-nested dataclasses, this also provides more
convenient `asdict` helpers.

To set this up, where previously the objects would be parsed from the
config via the `__init__` method, create a `build` classmethod that does
this and calls the dataclass initializer.

Some of the objects are mutated at runtime, in particular some of the
zones are mutated to set the color (this might be able to be refactored
out) and some of the camera functionality can be enabled/disabled.  Some
of the configs with `enabled` properties don't seem to have mqtt hooks
to be able to toggle this, in particular, the clips, snapshots, and
detect can be toggled but rtmp and record configs do not, but all of
these configs are still not frozen in case there is some other
functionality I am missing.

There are a couple other minor fixes here, one that was introduced
by me recently where `max_seconds` was not defined, the other to
properly `get()` the message payload when handling publishing mqtt
messages sent via websocket.
This commit is contained in:
Sean Vig 2021-05-22 18:28:15 -04:00 committed by Blake Blackshear
parent 1fbcf4d9b9
commit 84a0827aee
6 changed files with 651 additions and 831 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.DS_Store
*.pyc
*.swp
debug
.vscode
config/config.yml

File diff suppressed because it is too large Load Diff

View File

@ -109,6 +109,7 @@ class EventProcessor(threading.Thread):
earliest_event = datetime.datetime.now().timestamp()
# if the earliest event is more tha max seconds ago, cap it
max_seconds = self.config.clips.max_seconds
earliest_event = max(
earliest_event,
datetime.datetime.now().timestamp() - self.config.clips.max_seconds,

View File

@ -22,11 +22,11 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
if payload == "ON":
if not clips_settings.enabled:
logger.info(f"Turning on clips for {camera_name} via mqtt")
clips_settings._enabled = True
clips_settings.enabled = True
elif payload == "OFF":
if clips_settings.enabled:
logger.info(f"Turning off clips for {camera_name} via mqtt")
clips_settings._enabled = False
clips_settings.enabled = False
else:
logger.warning(f"Received unsupported value at {message.topic}: {payload}")
@ -44,11 +44,11 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
if payload == "ON":
if not snapshots_settings.enabled:
logger.info(f"Turning on snapshots for {camera_name} via mqtt")
snapshots_settings._enabled = True
snapshots_settings.enabled = True
elif payload == "OFF":
if snapshots_settings.enabled:
logger.info(f"Turning off snapshots for {camera_name} via mqtt")
snapshots_settings._enabled = False
snapshots_settings.enabled = False
else:
logger.warning(f"Received unsupported value at {message.topic}: {payload}")
@ -67,12 +67,12 @@ def create_mqtt_client(config: FrigateConfig, camera_metrics):
if not camera_metrics[camera_name]["detection_enabled"].value:
logger.info(f"Turning on detection for {camera_name} via mqtt")
camera_metrics[camera_name]["detection_enabled"].value = True
detect_settings._enabled = True
detect_settings.enabled = True
elif payload == "OFF":
if camera_metrics[camera_name]["detection_enabled"].value:
logger.info(f"Turning off detection for {camera_name} via mqtt")
camera_metrics[camera_name]["detection_enabled"].value = False
detect_settings._enabled = False
detect_settings.enabled = False
else:
logger.warning(f"Received unsupported value at {message.topic}: {payload}")

View File

@ -156,9 +156,9 @@ class TestConfig(TestCase):
}
frigate_config = FrigateConfig(config=config)
assert "dog" in frigate_config.cameras["back"].objects.filters
assert len(frigate_config.cameras["back"].objects.filters["dog"]._raw_mask) == 2
assert len(frigate_config.cameras["back"].objects.filters["dog"].raw_mask) == 2
assert (
len(frigate_config.cameras["back"].objects.filters["person"]._raw_mask) == 1
len(frigate_config.cameras["back"].objects.filters["person"].raw_mask) == 1
)
def test_ffmpeg_params_global(self):

View File

@ -100,7 +100,7 @@ def stop_ffmpeg(ffmpeg_process, logger):
def start_or_restart_ffmpeg(
ffmpeg_cmd, logger, logpipe: LogPipe, frame_size=None, ffmpeg_process=None
):
if not ffmpeg_process is None:
if ffmpeg_process is not None:
stop_ffmpeg(ffmpeg_process, logger)
if frame_size is None: