Skip to content

Code quality assessment: no changes#96

Draft
Copilot wants to merge 1 commit intodevelopmentfrom
copilot/assess-code-quality
Draft

Code quality assessment: no changes#96
Copilot wants to merge 1 commit intodevelopmentfrom
copilot/assess-code-quality

Conversation

Copy link
Contributor

Copilot AI commented Feb 23, 2026

Assessed overall code quality across the integration. No code changes were made — findings are documented below for prioritization.

Key Findings

  • Multi-instance unsafe hass.data: device, device_id, write_manager, register_manager stored at hass.data[DOMAIN] (global) instead of under entry_id — breaks with two simultaneous config entries
  • decode_value duplicated in sensor.py, cop_sensor.py, and tests/test_helpers.py; value_codec.py exists for exactly this purpose
  • Dual metadata system: sensor_meta.py (97 entries, 971 lines) and register map 6th-tuple element both define sensor metadata with no clear precedence rule
  • 13 bare except Exception catches across thz_device.py, __init__.py, calendar.py, config_flow.py
  • 20 functions > 50 lines; send_request() and _list_serial_ports() reach CC≈26
  • asyncio.sleep(0.01) held inside async with device.lock in 9 places — blocks other entities from device access during write operations
  • 15 failing tests: 13 due to missing pytest-asyncio (not in requirements_test.txt), 2 due to incomplete mock setup
  • Test-to-code ratio 0.37x; 13 production modules have no dedicated test file (calendar, cop_sensor, config_flow, number, switch, select, time, value_codec, others)
  • 86 auto-fixable lint errors (E501, W293); 4 internal development markdown files (BUGFIX_*.md, CODE_REVIEW_*.md, etc.) clutter repo root

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Assess code quality and suggest refactoring improvements Refactor: eliminate decode_value duplication, fix lint and minor code quality issues Feb 23, 2026
Copilot AI requested a review from bigbadoooff February 23, 2026 13:41
Copilot AI changed the title Refactor: eliminate decode_value duplication, fix lint and minor code quality issues Code quality assessment: no changes Feb 23, 2026
@bigbadoooff
Copy link
Owner

@copilot

analyze again with the current state of development branch

Copy link
Contributor Author

Copilot AI commented Feb 24, 2026

@copilot

analyze again with the current state of development branch

Here is the updated assessment against the current tip of development (commit 0f4d664).


Code Quality Assessment — development branch (2026-02-24)

What Changed Since Last Assessment

Four commits landed since the previous analysis:

Commit Change Quality impact
1d8340f Remove Monthly + Yearly COP sensors cop_sensor.py shrunk from 886 → 577 lines, 5 classes → 3
992b25b Fix area schema to use vol.In ✅ Minor correctness fix
710f2bb Add BLOCK_LABELS dict to const.py + translations ➡ Neutral — adds 61-line dict to const.py, improves UI
0f4d664 Fix ventilator speed unit/device_class ✅ Correctness fix in sensor_meta.py

Metrics at a Glance (dev branch vs previous)

Metric Previous Now Δ
Production lines 11,704 11,486 −218 ✅
Test functions 386 393 +7 ✅
Tests passing 371 378 +7 ✅
Tests failing 15 15 0 (same failures)
Lint errors (ruff) 86 80 −6 ✅
Broad except Exception 13 13 0
Functions > 50 lines 20 20 0
High-CC functions (≥10) 10 14 +4 ⚠️
Type hint coverage 80% 81% +1% ✅
Test/code ratio 0.37x 0.38x ≈same

Findings — Status by Category

✅ Resolved / Improved

  • cop_sensor.py reduced from 886 → 577 lines by removing unused Monthly/Yearly COP classes. The native_value CC≈11 offender from the previous report (the monthly/yearly classes) is now gone.
  • Ventilator speed sensor now has correct device_class and unit.
  • Area schema fixed to use vol.In (stricter, correct).
  • 7 new tests added, all passing.

⚠️ Still Open (unchanged from previous report)

  1. decode_value duplicated — still copy-pasted in sensor.py and cop_sensor.py. value_codec.py exists for this. The cop_sensor.py copy still has the comment "duplicated to avoid circular imports" even though importing from value_codec has no circularity at all.

  2. hass.data multi-instance safetydevice, device_id, write_manager, register_manager are still stored at hass.data[DOMAIN] (global, not per entry_id). A second config entry would overwrite the first. The duplicate setdefault is now actually three calls (lines 44, 86, 156) instead of two.

  3. sensor_meta.py — still 971 lines / 97 entries. The file's module docstring still claims to be the metadata store, while register map tuples also carry metadata via a 6th element. No single source of truth.

  4. 13 bare except Exception — identical count and locations as before. No narrowing of exception types.

  5. 20 functions > 50 lines — same list. send_request() (136 lines, CC≈26) and _list_serial_ports() (96 lines, CC≈26) remain the worst offenders.

  6. High-CC function count increased from 10 → 14_async_enable_integration_disabled_entities() (CC≈14) and should_hide_entity_by_default() (CC≈11, grown due to new BLOCK_LABELS content in const.py) are newly flagged. The function should_hide_entity_by_default at CC≈11 / 58 lines is itself a smell — hiding logic should be data-driven (a set of keywords), not a chain of if branches.

  7. asyncio.sleep(0.01) inside async with device.lock — still present in 9 places across switch.py, select.py, number.py, time.py, calendar.py, schedule.py.

  8. 15 failing tests — identical set. 13 fail due to pytest-asyncio not being in requirements_test.txt. 2 fail due to incomplete mock in test_thz_device_extended.py. These are non-functional tests that appear to be written but never run.

  9. has_entity_name only conditionally set (when a translation_key exists) — entity naming is inconsistent between translated and untranslated entities.

  10. 1 live f-string in a _LOGGER callthz_device.py:630 uses _LOGGER.error(f"...") instead of _LOGGER.error("...", e). 6 commented-out _LOGGER debug lines also remain in thz_device.py.

  11. 4 internal markdown files in repo rootBUGFIX_CONNECTION_TIMEOUT.md, CODE_REVIEW_SUMMARY.md, IMPLEMENTATION_SUMMARY.md, REFACTORING_SUMMARY.md are developer artefacts, not user docs. Should move to docs/ or be removed.


Priority Order for Next Steps

Priority Item
High Fix hass.data global-vs-per-entry (multi-instance safety)
High Add pytest-asyncio to requirements_test.txt to un-fail 13 tests
Medium Consolidate decode_value into value_codec.py
Medium Narrow the 13 except Exception to specific types
Medium Make should_hide_entity_by_default data-driven (set of keywords/prefixes) to reduce CC
Low Move the 4 internal .md files out of repo root
Low Fix the lone f-string logger call + remove 6 commented-out debug lines in thz_device.py

Overall grade: C+ → B− (slight improvement due to COP cleanup and test additions, but the structural issues remain untouched).

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