Skip to content

Conversation

@mode0192
Copy link

This PR fixes several issues in custom_components/smartir/fan.py that can lead to incorrect state after restart and incorrect oscillation behavior for toggle-style IR codes.

What changed

  • Restart restore: Restore fan state from percentage when speed is not available (HA versions may persist percentage instead of speed). If HA restores state=on without speed/percentage, fall back to last_on_speed (or first speed).
  • Oscillation: SmartIR fan JSON files often provide a single oscillate command that toggles oscillation. The integration now sends the oscillate command only when the desired oscillation state changes, and no longer lets oscillation “override” speed commands.
  • Power sensor: Ensure the power sensor listener is always registered when configured, and avoid setting speed=None when the sensor indicates the device is turned on externally (prevents breaking percentage/commands).
  • Config validation: Treat delay as a float (cv.positive_float) and cast to float before passing to the controller.

Why

  • Users reported the fan showing OFF after Home Assistant restart even when it was ON previously (because HA restores percentage instead of speed in some cases).
  • Speed changes while oscillation is enabled could unintentionally toggle oscillation or prevent speed commands from being sent correctly.
  • Power sensor tracking could fail to initialize depending on restore-state availability, and setting speed to None breaks percentage/command logic.

How to test

  1. Turn fan ON, set a speed, restart Home Assistant → entity should restore as ON with correct speed/percentage.
  2. Enable oscillation, then change speed → oscillation should not toggle.
  3. Toggle oscillation on/off from the UI → IR command should send only when changing state.
  4. If configured with a power sensor, turn the fan on/off via remote → entity should update reliably.

* Treat `delay` config as a float (`cv.positive_float`) and cast in `__init__` to avoid string delays reaching the controller.
* Fix state restoration after Home Assistant restart by restoring from `percentage` when `speed` is not present, and falling back to `last_on_speed` when HA restores `state=on` without attributes.
* Fix oscillation handling for toggle-style IR codes: send the `oscillate` command only when the requested oscillation state differs, and avoid sending oscillate commands from `send_command()` (prevents speed changes from toggling oscillation).
* Improve power sensor behavior:

  * Always register the power sensor listener when configured (even when no restored state exists).
  * Avoid setting `speed=None` on remote power-on (keeps percentage/commands stable).
  * Use a synchronous `@callback` handler for the sensor event.

Testing:

* Verified fan restores as ON after HA restart when previously ON.
* Verified changing speed does not toggle oscillation, and oscillation toggles only when requested.
* Verified power sensor remote ON/OFF updates entity state without breaking percentage.
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.

1 participant