Skip to content

Add code quality tooling and improve test fixtures#34

Open
raman325 wants to merge 10 commits intokoush:mainfrom
raman325:code-quality-improvements
Open

Add code quality tooling and improve test fixtures#34
raman325 wants to merge 10 commits intokoush:mainfrom
raman325:code-quality-improvements

Conversation

@raman325
Copy link
Copy Markdown
Contributor

@raman325 raman325 commented Dec 9, 2025

Summary

This is a code quality PR with no functional changes to the integration itself. All changes focus on improving development workflow, code maintainability, and CI/CD.

Changes Made

GitHub Actions Workflows

  • Added hacs.yaml - HACS validation workflow
  • Added hassfest.yaml - Home Assistant integration validation
  • Added pytest.yaml - Test runner with coverage reporting to Codecov
  • Added lint.yaml - Linting via ruff
  • Added markdownlint.yaml - Markdown linting workflow
  • Added .codecov.yaml - Codecov configuration

Pre-commit & Linting

  • Added .pre-commit-config.yaml with ruff, pydocstyle, yamllint, mypy, actionlint, markdownlint
  • Added pyproject.toml with consolidated tool configuration
  • Added .markdownlint.json for markdown linting configuration
  • Split requirements into requirements_lint.txt, requirements_test.txt, requirements_dev.txt

Development Environment

  • Added devcontainer configuration for developing within a Home Assistant instance which sets integration log level to debug and enables the debugpy integration by default
  • Added Home Assistant VS Code extension (keesschollaart.vscode-home-assistant) to devcontainer
  • Added .editorconfig for consistent formatting
  • Added development setup instructions to README

Test Infrastructure Improvements

  • Added comprehensive tests for http.py module (~600 lines of new tests)
    • Tests for retrieve_token() function
    • Tests for ScryptedView class and URL creation
    • Tests for helper functions (_is_websocket, _response_header, _init_header)
    • Tests for static file serving and WebSocket handling
  • Added shared test constants in tests/const.py to reduce duplication
  • Added JSON fixture files for login responses (login_success.json, login_error_*.json)
  • Refactored conftest.py with reusable fixtures:
    • load_fixture() helper and fixture loading functions
    • HTTP module fixtures (mock_web_request, scrypted_view, etc.)
    • Replaced _register_scrypted_flow with auto_enable_custom_integrations
    • Switched from monkeypatch to unittest.mock.patch context managers for better async support
    • Created reusable mock fixtures (error scenarios, panel lifecycle, etc.) for common test patterns
  • Configured asyncio_mode = "auto" to eliminate @pytest.mark.asyncio decorators
  • Changed import style from import custom_components.scrypted as scrypted to from custom_components import scrypted per PEP 8
  • Coverage threshold set to 80% in .coveragerc

Code Modernization (linter-driven, no functional changes)

  • Modernized http.py to use pathlib.Path instead of os.path
  • Extracted magic strings to constants (DEFAULT_SCRYPTED_PORT, filenames)
  • Use asyncio.get_running_loop() instead of deprecated session.loop
  • Removed unused token parameter from _async_unregister_lovelace_resource()
  • Removed unused created_resource variable that was assigned but never read
  • Renamed unused config_entry to _config_entry in async_get_options_flow()
  • Fixed raise-missing-from pylint errors - changed from generic except Exception with isinstance check to specific except ClientConnectorError with from e for proper exception chaining per PEP 3134
  • Fixed file operations to use context managers with encoding (with open(..., encoding="utf-8"))
  • Fixed shadowed builtin (type parameter renamed to selector_type)
  • Improved import scoping in config_flow.py - directly import specific classes from homeassistant.helpers.selector submodules
  • Fixed pydocstyle errors (blank lines after docstrings)
  • Fixed mypy errors (use dict literals for type inference)
  • Applied ruff formatting to Python files
  • Removed unused __init__ in ScryptedConfigFlow
  • Renamed panelconf to panel_conf for snake_case consistency
  • Fixed markdown formatting in README.md, TODO.md, and bug_report.md

Note for @koush

GitHub Actions: The CI workflows require GitHub Actions to be enabled on the repository. Go to Settings → Actions → General and ensure "Allow all actions and reusable workflows" is selected.

Codecov: The pytest workflow uses Codecov for coverage reporting. To enable this:

  1. Go to codecov.io and sign in with GitHub
  2. Add the koush/ha_scrypted repository
  3. Copy the repository upload token
  4. Add it as a secret named CODECOV_TOKEN in GitHub repository settings

The workflow will still run without the token (coverage upload will just be skipped).

Test Plan

  • All existing tests pass
  • Pre-commit hooks run successfully
  • GitHub Actions workflows complete without errors
  • Devcontainer starts Home Assistant successfully

🤖 Generated with Claude Code

- Add GitHub Actions workflows (hacs, hassfest, pytest with codecov)
- Add pre-commit configuration with ruff, pylint, yamllint, mypy
- Add devcontainer for development within Home Assistant instance
- Refactor test fixtures to use unittest.mock.patch instead of monkeypatch
- Add pyproject.toml with consolidated tool configuration
- Split requirements into lint, test, and dev files
- Fix pylint errors (raise-from, file context managers, shadowed builtins)
- Add .editorconfig for consistent formatting
- Update README with development setup instructions

No functional changes to the integration itself.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor Author

@raman325 raman325 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review: Explaining the code changes made in this PR. All changes are linter-driven and don't affect functionality.

Copy link
Copy Markdown
Contributor Author

@raman325 raman325 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional comment for unused variable removal.

Copy link
Copy Markdown
Contributor Author

@raman325 raman325 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional comment for import and test changes.

raman325 and others added 5 commits December 9, 2025 09:46
Removed .codex and .ha from the .gitignore file.
- Upgrade pre-commit hook for markdownlint-cli (fix hook id)
- Add .markdownlint.json config file
- Add separate markdownlint.yaml workflow for markdown linting
- Fix markdown formatting in README.md, TODO.md, and bug_report.md
- Fix pydocstyle errors (blank lines after docstrings)
- Fix mypy errors (use dict literals for type inference)
- Apply ruff formatting to Python files
- Remove unused __init__ in ScryptedConfigFlow
- Rename panelconf to panel_conf for snake_case consistency

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add comprehensive tests for custom_components/scrypted/http.py (~600 lines)
  - Tests for retrieve_token() function
  - Tests for ScryptedView class and URL creation
  - Tests for helper functions (_is_websocket, _response_header, _init_header)
  - Tests for static file serving and WebSocket handling

- Add shared test constants in tests/const.py to reduce duplication

- Add JSON fixture files for login responses:
  - login_success.json
  - login_error_not_logged_in.json
  - login_error_incorrect_password.json

- Refactor conftest.py:
  - Add load_fixture() helper and fixture loading functions
  - Add HTTP module fixtures (mock_web_request, scrypted_view, etc.)
  - Replace _register_scrypted_flow with auto_enable_custom_integrations

- Modernize http.py:
  - Use pathlib.Path instead of os.path for file operations
  - Extract magic strings to constants (DEFAULT_SCRYPTED_PORT, filenames)
  - Use asyncio.get_running_loop() instead of session.loop

- Clean up unused parameters:
  - Remove unused token param from _async_unregister_lovelace_resource()
  - Rename unused config_entry to _config_entry in async_get_options_flow()

- Update coverage configuration:
  - Move fail_under threshold to .coveragerc (80%)
  - Remove --cov-fail-under from pytest addopts

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
raman325 and others added 2 commits December 11, 2025 12:34
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ements

* upstream/main:
  Add frontend dependency to ensure proper initialization order (koush#37)
@raman325
Copy link
Copy Markdown
Contributor Author

I successfully tested:

  • navigating to sidepanel whether it was mgmt console or NVR
  • toggling resource registration
  • clicking through NVR card into side panel

Couldn't think of anything else to test. I added a review comment for every "code change" (anything beyond a formatting change) in the integration in case you are curious why any of those changes were made. The majority of the PR is related to tests, pre-commit/CI, and reformatting/addressing pre-commit failures

@raman325 raman325 marked this pull request as ready for review December 13, 2025 03:28
Copilot AI review requested due to automatic review settings December 13, 2025 03:28
@raman325 raman325 marked this pull request as draft December 13, 2025 03:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces comprehensive code quality tooling and testing infrastructure with no functional changes to the Scrypted integration. It establishes CI/CD workflows, linting standards, pre-commit hooks, and a complete development environment setup via devcontainer.

Key changes include:

  • Added GitHub Actions workflows for testing (pytest with Codecov), linting (ruff), Home Assistant validation (hassfest, HACS), and markdown linting
  • Migrated from monkeypatch to unittest.mock.patch for better async test support and added ~600 lines of new HTTP module tests
  • Consolidated tool configuration in pyproject.toml with coverage threshold set to 80%
  • Refactored code to use modern Python patterns (pathlib, proper exception chaining, context managers)

Reviewed changes

Copilot reviewed 38 out of 40 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
.github/workflows/*.yaml Added CI workflows for pytest, linting, HACS/hassfest validation, and markdown linting
.github/.codecov.yaml Codecov configuration with 70-100% coverage range
.devcontainer/* Complete devcontainer setup with debugpy support and auto-configuration
.pre-commit-config.yaml Pre-commit hooks for ruff, pydocstyle, yamllint, mypy, actionlint, markdownlint
pyproject.toml Consolidated configuration for ruff, mypy, pylint, and pytest
tests/conftest.py Refactored from monkeypatch to unittest.mock with reusable fixture library
tests/test_http.py New comprehensive test suite (~600 lines) for HTTP module
tests/fixtures/*.json JSON fixtures for login response testing
tests/const.py Shared test constants to reduce duplication
tests/test_*.py Updated to use new fixtures and removed @pytest.mark.asyncio decorators
custom_components/scrypted/__init__.py Removed unused variables, improved exception handling, renamed panelconf to panel_conf
custom_components/scrypted/http.py Modernized with pathlib, extracted constants, improved file handling
custom_components/scrypted/config_flow.py Fixed shadowed builtin (type→selector_type), improved import scoping
custom_components/scrypted/const.py Added DEFAULT_SCRYPTED_PORT and filename constants
requirements_*.txt Split into test, lint, and dev requirements
README.md Added development setup instructions
TODO.md Reformatted with proper markdown headings

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@raman325 raman325 marked this pull request as ready for review December 14, 2025 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants