From 40398420454d2aff92b8144d0e136e6bab84a599 Mon Sep 17 00:00:00 2001 From: Teemu R Date: Mon, 18 Sep 2023 16:24:53 +0200 Subject: [PATCH] Fix invalid cache handling for miotcloud schema fetch (#1819) The cache read raises an exception instead of returning `None` when no schema file has been downloaded. --- miio/miot_cloud.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/miio/miot_cloud.py b/miio/miot_cloud.py index a596b3e10..ee38dd65d 100644 --- a/miio/miot_cloud.py +++ b/miio/miot_cloud.py @@ -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)) @@ -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)