Skip to content

Commit

Permalink
Fix invalid cache handling for miotcloud schema fetch (#1819)
Browse files Browse the repository at this point in the history
The cache read raises an exception instead of returning `None` when no
schema file has been downloaded.
  • Loading branch information
rytilahti authored Sep 18, 2023
1 parent f7b554f commit 4039842
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions miio/miot_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ def get_release_list(self) -> ReleaseList:
def get_device_model(self, model: str) -> DeviceModel:
"""Get device model for model name."""
file = self._cache_dir / f"{model}.json"
spec = self._file_from_cache(file)
if spec is not None:
try:
spec = self._file_from_cache(file)
return DeviceModel.parse_obj(spec)
except FileNotFoundError:
_LOGGER.debug("Unable to find schema file %s, going to fetch" % file)

return DeviceModel.parse_obj(self.get_model_schema(model))

Expand All @@ -96,9 +98,11 @@ def get_model_schema(self, model: str) -> Dict:
release_info = specs.info_for_model(model)

model_file = self._cache_dir / f"{release_info.model}.json"
spec = self._file_from_cache(model_file)
if spec is not None:
try:
spec = self._file_from_cache(model_file)
return spec
except FileNotFoundError:
_LOGGER.debug(f"Cached schema not found for {model}, going to fetch it")

spec = MiotSpec.get_spec_for_urn(device_urn=release_info.type)
self._write_to_cache(model_file, spec)
Expand Down

0 comments on commit 4039842

Please sign in to comment.