Skip to content

Commit

Permalink
fix: migrate more deprecated pydantic calls (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 11, 2024
1 parent 8fc27a3 commit 50ef161
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
26 changes: 16 additions & 10 deletions src/uiprotect/data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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
]

Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/uiprotect/data/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/uiprotect/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 50ef161

Please sign in to comment.