Skip to content

refactor: Extract startup() into 8 named submethods#1951

Open
cl445 wants to merge 1 commit intoKartoffelToby:developfrom
cl445:refactor/phase5-startup-split
Open

refactor: Extract startup() into 8 named submethods#1951
cl445 wants to merge 1 commit intoKartoffelToby:developfrom
cl445:refactor/phase5-startup-split

Conversation

@cl445
Copy link
Contributor

@cl445 cl445 commented Feb 26, 2026

Summary

  • Break the monolithic startup() (~888 lines) into a slim orchestrator (~22 lines) and 8 focused private methods with strict type annotations
  • Eliminate the ContinueLoop exception anti-pattern
  • Fix type inconsistencies found during extraction (_current_humidity, bt_min_temp/bt_max_temp, attribute restore casts)

Changes

New orchestrator (startup())

async def startup(self) -> None:
    while self.startup_running:
        sensor_state = self.hass.states.get(self.sensor_entity_id)
        if not self._check_entities_ready(sensor_state):
            await asyncio.sleep(20)
            continue
        states = self._collect_trv_states()
        self._resolve_temperature_range(states)
        self._initialize_sensors(sensor_state)
        await self._restore_state(states)
        self._validate_hvac_mode(states)
        await self._initialize_trvs()
        await self._finalize_startup()
        break

8 extracted submethods

# Method Sync/Async Responsibility
1 _check_entities_ready(sensor_state: State | None) -> bool sync Sensor + TRV availability check
2 _collect_trv_states() -> list[State] sync Gather TRV + optional cooler states
3 _resolve_temperature_range(states: list[State]) sync Min/max/step from TRV states
4 _initialize_sensors(sensor_state: State | None) sync Room temp, humidity, cooler, window
5 _restore_state(states: list[State]) async Restore from async_get_last_state
6 _validate_hvac_mode(states: list[State]) sync HVAC mode validation + initial HA state
7 _initialize_trvs() async TRV init, tweak, calibration, control
8 _finalize_startup() async Triggers, listeners, periodic tasks

Type fixes

  • _current_humidity: 0 (int) → 0.0: float — matches current_humidity property return type
  • bt_min_temp/bt_max_temp: 0/30 (int) → 0.0/30.0: float — matches reduce_attribute() output
  • Explicit bool(), float(), str() casts on attributes.get() restores
  • Removed redundant convert_to_float() on already-float value

Test plan

  • 37 new tests in tests/unit/test_climate_startup.py covering submethods 1–6
  • Full suite: 911 passed
  • Manual HA integration test
Break the monolithic startup() (~888 lines) into a slim orchestrator
(~22 lines) and 8 focused private methods:

- _check_entities_ready(sensor_state) -> bool
- _collect_trv_states() -> list[State]
- _resolve_temperature_range(states)
- _initialize_sensors(sensor_state)
- _restore_state(states) [async]
- _validate_hvac_mode(states)
- _initialize_trvs() [async]
- _finalize_startup() [async]

All methods use strict type annotations (State, list[State], etc.).
Eliminates the ContinueLoop exception anti-pattern.

Fixes type inconsistencies found during extraction:
- _current_humidity: int(0) -> float(0.0), matching property return type
- bt_min_temp/bt_max_temp: int -> float, matching reduce_attribute output
- Explicit casts for attributes.get() restores (bool, float, str)
- Redundant convert_to_float on already-float value removed

Adds 37 unit tests in tests/unit/test_climate_startup.py covering
submethods 1-6. Full suite: 911 passed.
@coderabbitai
Copy link

coderabbitai bot commented Feb 26, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cl445 cl445 marked this pull request as ready for review February 26, 2026 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant