From ddfcbc7b48aa89fa5d2f1d0b60ddb5ed34959af7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 11 Dec 2024 23:30:32 +0100 Subject: [PATCH] fix: migrate more deprecated pydantic calls --- src/uiprotect/data/base.py | 26 ++++++++++++++++---------- src/uiprotect/data/bootstrap.py | 6 +++--- src/uiprotect/utils.py | 2 +- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/uiprotect/data/base.py b/src/uiprotect/data/base.py index 18b04bd8..ce50a637 100644 --- a/src/uiprotect/data/base.py +++ b/src/uiprotect/data/base.py @@ -137,10 +137,12 @@ def from_unifi_dict( data.pop("api", None) return cls(api=api, **data) - return cls.construct(**data) + return cls.model_construct(**data) @classmethod - def construct(cls, _fields_set: set[str] | None = None, **values: Any) -> Self: + def model_construct( + cls, _fields_set: set[str] | None = None, **values: Any + ) -> Self: api: ProtectApiClient | None = values.pop("api", None) ( unifi_objs, @@ -152,19 +154,21 @@ def construct(cls, _fields_set: set[str] | None = None, **values: Any) -> Self: ) = cls._get_protect_model() for key, value in values.items(): if has_unifi_objs and key in unifi_objs and isinstance(value, dict): - values[key] = unifi_objs[key].construct(**value) + values[key] = unifi_objs[key].model_construct(**value) elif has_unifi_lists and key in unifi_lists and isinstance(value, list): values[key] = [ - unifi_lists[key].construct(**v) if isinstance(v, dict) else v + unifi_lists[key].model_construct(**v) if isinstance(v, dict) else v for v in value ] elif has_unifi_dicts and key in unifi_dicts and isinstance(value, dict): values[key] = { - k: unifi_dicts[key].construct(**v) if isinstance(v, dict) else v + k: unifi_dicts[key].model_construct(**v) + if isinstance(v, dict) + else v for k, v in value.items() } - obj = super().construct(_fields_set=_fields_set, **values) + obj = super().model_construct(_fields_set=_fields_set, **values) if api is not None: obj._api = api @@ -369,7 +373,7 @@ def _unifi_dict_protect_obj( if isinstance(value, ProtectBaseObject): value = value.unifi_dict() elif isinstance(value, dict): - value = klass.construct({}).unifi_dict(data=value) # type: ignore[arg-type] + value = klass.model_construct({}).unifi_dict(data=value) # type: ignore[arg-type] return value @@ -390,7 +394,7 @@ def _unifi_dict_protect_obj_list( return [ item.unifi_dict() if isinstance(item, ProtectBaseObject) - else klass.construct({}).unifi_dict(data=item) # type: ignore[arg-type] + else klass.model_construct({}).unifi_dict(data=item) # type: ignore[arg-type] for item in value ] @@ -587,9 +591,11 @@ def __init__(self, **data: Any) -> None: self._update_sync = update_sync or UpdateSynchronization() @classmethod - def construct(cls, _fields_set: set[str] | None = None, **values: Any) -> Self: + def model_construct( + cls, _fields_set: set[str] | None = None, **values: Any + ) -> Self: update_sync = values.pop("update_sync", None) - obj = super().construct(_fields_set=_fields_set, **values) + obj = super().model_construct(_fields_set=_fields_set, **values) obj._update_sync = update_sync or UpdateSynchronization() return obj diff --git a/src/uiprotect/data/bootstrap.py b/src/uiprotect/data/bootstrap.py index 94980343..2f76bd8f 100644 --- a/src/uiprotect/data/bootstrap.py +++ b/src/uiprotect/data/bootstrap.py @@ -426,7 +426,7 @@ def _process_ws_keyring_or_ulp_user_message( if updated_obj is None: return None - old_obj = updated_obj.copy() + old_obj = updated_obj.model_copy() updated_data = {to_snake_case(k): v for k, v in data.items()} updated_obj.update_from_dict(updated_data) @@ -461,7 +461,7 @@ def _process_nvr_update( if not (data := self.nvr.unifi_dict_to_dict(data)): return None - old_nvr = self.nvr.copy() + old_nvr = self.nvr.model_copy() self.nvr = self.nvr.update_from_dict(data) return WSSubscriptionMessage( @@ -517,7 +517,7 @@ def _process_device_update( # nothing left to process return None - old_obj = obj.copy() + old_obj = obj.model_copy() obj = obj.update_from_dict(data) if model_type is ModelType.EVENT: diff --git a/src/uiprotect/utils.py b/src/uiprotect/utils.py index cce66878..a4ff07b8 100644 --- a/src/uiprotect/utils.py +++ b/src/uiprotect/utils.py @@ -572,7 +572,7 @@ def local_datetime(dt: datetime | None = None) -> datetime: def log_event(event: Event) -> None: from uiprotect.data import EventType - _LOGGER.debug("event WS msg: %s", event.dict()) + _LOGGER.debug("event WS msg: %s", event.model_dump()) if "smart" not in event.type.value: return