diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 095343f..0b632e4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -82,6 +82,14 @@ repos: - id: markdownlint - repo: local hooks: + - id: parsetest + name: "Generate AirOS Fixtures" + entry: script/run-in-env.sh python script/generate_ha_fixture.py + language: script + types: [python] + # Run when Python changes (to refresh against code changes) and when userdata JSON changes + files: ^(airos|tests|script)/.+\.py$|^fixtures/userdata/.+\.json$ + pass_filenames: false - id: pytest name: "pytest" entry: script/run-in-env.sh pytest diff --git a/airos/airos8.py b/airos/airos8.py index 6afbd8e..18beeae 100644 --- a/airos/airos8.py +++ b/airos/airos8.py @@ -193,7 +193,8 @@ async def login(self) -> bool: _LOGGER.info("Login task was cancelled") raise - def derived_data(self, response: dict[str, Any]) -> dict[str, Any]: + @staticmethod + def derived_data(response: dict[str, Any]) -> dict[str, Any]: """Add derived data to the device response.""" derived: dict[str, Any] = { "station": False, diff --git a/script/generate_ha_fixture.py b/script/generate_ha_fixture.py index 60276de..74efc5a 100644 --- a/script/generate_ha_fixture.py +++ b/script/generate_ha_fixture.py @@ -45,7 +45,7 @@ def generate_airos_fixtures() -> None: with open(base_fixture_path, encoding="utf-8") as source: source_data = json.loads(source.read()) - derived_data = AirOS.derived_data(None, source_data) # type: ignore[arg-type] + derived_data = AirOS.derived_data(source_data) new_data = AirOSData.from_dict(derived_data) with open(new_fixture_path, "w", encoding="utf-8") as new: diff --git a/script/mashumaro-step-debug.py b/script/mashumaro-step-debug.py index 33f816e..8f201be 100644 --- a/script/mashumaro-step-debug.py +++ b/script/mashumaro-step-debug.py @@ -12,6 +12,7 @@ if _project_root_dir not in sys.path: sys.path.append(_project_root_dir) +from airos.airos8 import AirOS # noqa: E402 from airos.data import AirOS8Data, Interface, Remote, Station, Wireless # noqa: E402 logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) @@ -71,14 +72,17 @@ def main() -> None: interface_obj = Interface.from_dict(interface_data) # noqa: F841 _LOGGER.info(" Success! Interface is valid.") + _LOGGER.info("Deriving AirOS8Data from object...") + derived_data = AirOS.derived_data(data) + _LOGGER.info("Attempting to deserialize full AirOS8Data object...") - airos_data_obj = AirOS8Data.from_dict(data) # noqa: F841 + airos_data_obj = AirOS8Data.from_dict(derived_data) # noqa: F841 _LOGGER.info("Success! Full AirOS8Data object is valid.") - except Exception as e: + except Exception: _LOGGER.info("\n------------------") _LOGGER.info("CRITICAL ERROR FOUND!") - _LOGGER.info("The program failed at: %s", e) + _LOGGER.exception("The program failed") _LOGGER.info("------------------\n")