Skip to content

Commit 07e20dd

Browse files
committed
Share client session for update check
1 parent 7a30700 commit 07e20dd

File tree

1 file changed

+62
-47
lines changed
  • matter_server/server/ota

1 file changed

+62
-47
lines changed

matter_server/server/ota/dcl.py

+62-47
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,38 @@
1212
LOGGER = logging.getLogger(__name__)
1313

1414

15-
async def _get_software_versions(vid: int, pid: int) -> Any:
15+
async def _get_software_versions(session: ClientSession, vid: int, pid: int) -> Any:
1616
"""Check DCL if there are updates available for a particular node."""
17-
async with ClientSession(raise_for_status=False) as http_session:
18-
# fetch the paa certificates list
19-
async with http_session.get(
20-
f"{DCL_PRODUCTION_URL}/dcl/model/versions/{vid}/{pid}"
21-
) as response:
22-
if response.status == HTTPStatus.NOT_FOUND:
23-
return None
24-
response.raise_for_status()
25-
return await response.json()
17+
# fetch the paa certificates list
18+
async with session.get(f"/dcl/model/versions/{vid}/{pid}") as response:
19+
if response.status == HTTPStatus.NOT_FOUND:
20+
return None
21+
response.raise_for_status()
22+
return await response.json()
2623

2724

28-
async def _get_software_version(vid: int, pid: int, software_version: int) -> Any:
25+
async def _get_software_version(
26+
session: ClientSession, vid: int, pid: int, software_version: int
27+
) -> Any:
2928
"""Check DCL if there are updates available for a particular node."""
30-
async with ClientSession(raise_for_status=True) as http_session:
31-
# fetch the paa certificates list
32-
async with http_session.get(
33-
f"{DCL_PRODUCTION_URL}/dcl/model/versions/{vid}/{pid}/{software_version}"
34-
) as response:
35-
return await response.json()
29+
# fetch the paa certificates list
30+
async with session.get(
31+
f"/dcl/model/versions/{vid}/{pid}/{software_version}"
32+
) as response:
33+
response.raise_for_status()
34+
return await response.json()
3635

3736

3837
async def _check_update_version(
38+
session: ClientSession,
3939
vid: int,
4040
pid: int,
4141
current_software_version: int,
4242
requested_software_version: int,
4343
requested_software_version_string: str | None = None,
4444
) -> None | dict:
4545
version_res: dict = await _get_software_version(
46-
vid, pid, requested_software_version
46+
session, vid, pid, requested_software_version
4747
)
4848
if not isinstance(version_res, dict):
4949
raise TypeError("Unexpected DCL response.")
@@ -81,39 +81,54 @@ async def check_for_update(
8181
) -> None | dict:
8282
"""Check if there is a software update available on the DCL."""
8383
try:
84-
# If a specific version as integer is requested, just fetch it (and hope it exists)
85-
if isinstance(requested_software_version, int):
86-
return await _check_update_version(
87-
vid, pid, current_software_version, requested_software_version
88-
)
89-
90-
# Get all versions and check each one of them.
91-
versions = await _get_software_versions(vid, pid)
92-
if versions is None:
93-
LOGGER.info("There is no update information for this device on the DCL.")
94-
return None
84+
async with ClientSession(
85+
base_url=DCL_PRODUCTION_URL, raise_for_status=False
86+
) as session:
87+
# If a specific version as integer is requested, just fetch it (and hope it exists)
88+
if isinstance(requested_software_version, int):
89+
return await _check_update_version(
90+
session,
91+
vid,
92+
pid,
93+
current_software_version,
94+
requested_software_version,
95+
)
96+
97+
# Get all versions and check each one of them.
98+
versions = await _get_software_versions(session, vid, pid)
99+
if versions is None:
100+
LOGGER.info(
101+
"There is no update information for this device on the DCL."
102+
)
103+
return None
95104

96-
all_software_versions: list[int] = versions["modelVersions"]["softwareVersions"]
97-
newer_software_versions = [
98-
version
99-
for version in all_software_versions
100-
if version > current_software_version
101-
]
105+
all_software_versions: list[int] = versions["modelVersions"][
106+
"softwareVersions"
107+
]
108+
newer_software_versions = [
109+
version
110+
for version in all_software_versions
111+
if version > current_software_version
112+
]
113+
114+
# Check if there is a newer software version available, no downgrade possible
115+
if not newer_software_versions:
116+
return None
102117

103-
# Check if there is a newer software version available, no downgrade possible
104-
if not newer_software_versions:
105-
LOGGER.info("No newer software version available.")
118+
# Check if latest firmware is applicable, and backtrack from there
119+
for version in sorted(newer_software_versions, reverse=True):
120+
if version_candidate := await _check_update_version(
121+
session,
122+
vid,
123+
pid,
124+
current_software_version,
125+
version,
126+
requested_software_version,
127+
):
128+
return version_candidate
129+
LOGGER.debug("Software version %d not applicable.", version)
106130
return None
107131

108-
# Check if latest firmware is applicable, and backtrack from there
109-
for version in sorted(newer_software_versions, reverse=True):
110-
if version_candidate := await _check_update_version(
111-
vid, pid, current_software_version, version, requested_software_version
112-
):
113-
return version_candidate
114-
LOGGER.debug("Software version %d not applicable.", version)
115-
return None
116-
117132
except (ClientError, TimeoutError) as err:
118133
raise UpdateCheckError(
119134
f"Fetching software versions from DCL for device with vendor id {vid} product id {pid} failed."

0 commit comments

Comments
 (0)