Skip to content

Commit

Permalink
chore: update to pydantic 2 with backward compat
Browse files Browse the repository at this point in the history
This commit essentially reverts the last, as I have
learned that one can essentially support both pydantic
1 and 2 - which will make packaging work for both
Home Assistant Core, and nixpkgs.
  • Loading branch information
jnsgruk committed Aug 24, 2024
1 parent 10ad8d8 commit 0016faf
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pytouchlinesl"
version = "0.1.2"
version = "0.1.3"
authors = [{ name = "Jon Seager", email = "[email protected]" }]
description = "An API client for Roth's TouchlineSL control system."
readme = "README.md"
Expand All @@ -10,7 +10,7 @@ classifiers = [
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
]
dependencies = ["aiohttp", "pydantic==1.10.17"]
dependencies = ["aiohttp", "pydantic"]

[project.optional-dependencies]
dev = ["pytest", "pytest-asyncio", "ruff"]
Expand Down
24 changes: 18 additions & 6 deletions pytouchlinesl/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ async def set_zone_schedule(self, module_id: str, zone_id: int, schedule_id: int
or z.zone.id == zone_id
]

# pydantic v1 compatibility
try:
p0_intervals = [
i.dict(by_alias=True) for i in schedule.p0_intervals if i.start != 6100
]
p1_intervals = [
i.dict(by_alias=True) for i in schedule.p1_intervals if i.start != 6100
]
except AttributeError:
p0_intervals = [
i.model_dump(by_alias=True) for i in schedule.p0_intervals if i.start != 6100
]
p1_intervals = [
i.model_dump(by_alias=True) for i in schedule.p1_intervals if i.start != 6100
]

# TODO: This be simplified with Pydantic models
data = {
"scheduleName": schedule.name,
Expand All @@ -142,14 +158,10 @@ async def set_zone_schedule(self, module_id: str, zone_id: int, schedule_id: int
"p0SetbackTemp": schedule.p0_setback_temp,
# Empty intervals start/end with a value of 6100, which is invalid
# if sent in this request.
"p0Intervals": [
i.dict(by_alias=True) for i in schedule.p0_intervals if i.start != 6100
],
"p0Intervals": p0_intervals,
"p1Days": schedule.p1_days,
"p1SetbackTemp": schedule.p1_setback_temp,
"p1Intervals": [
i.dict(by_alias=True) for i in schedule.p1_intervals if i.start != 6100
],
"p1Intervals": p1_intervals,
},
}

Expand Down
5 changes: 4 additions & 1 deletion pytouchlinesl/client/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

from typing import Any, Dict, List, Literal, Optional

from pydantic import BaseModel, Field
try:
from pydantic.v1 import BaseModel, Field
except ImportError:
from pydantic import BaseModel, Field


class ZoneFlagsModel(BaseModel):
Expand Down
Loading

0 comments on commit 0016faf

Please sign in to comment.