Skip to content

Move Tuya listener classes to separate module#168636

Draft
epenet wants to merge 8 commits intodevfrom
epenet/20260420-1735
Draft

Move Tuya listener classes to separate module#168636
epenet wants to merge 8 commits intodevfrom
epenet/20260420-1735

Conversation

@epenet
Copy link
Copy Markdown
Contributor

@epenet epenet commented Apr 20, 2026

Proposed change

  • Move DeviceListener, TokenListener classes and TuyaConfigEntry type alias to coordinator.py
  • Adjust the runtime_data to be the DeviceListener (the manager can be accessed via the corresponding attribute)
  • Add a async_create_listener helper to create the TokenListener, the Manager and the DeviceListener

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Copilot AI review requested due to automatic review settings April 20, 2026 15:38
@home-assistant home-assistant bot added cla-signed code-quality integration: tuya Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage Top 50 Integration is ranked within the top 50 by usage labels Apr 20, 2026
Copy link
Copy Markdown
Contributor

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

Refactors the Tuya integration by moving listener classes into a dedicated module and simplifying what is stored in ConfigEntry.runtime_data.

Changes:

  • Added coordinator.py to host DeviceListener, TokenListener, and the TuyaConfigEntry type alias.
  • Updated Tuya platform modules to import TuyaConfigEntry from coordinator.py instead of __init__.py.
  • Simplified entry.runtime_data to store only the listener and adjusted unload logic to access the manager via listener.manager.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
homeassistant/components/tuya/init.py Uses listener-only runtime_data and imports listener classes/type alias from coordinator.py.
homeassistant/components/tuya/coordinator.py New module containing DeviceListener/TokenListener and TuyaConfigEntry.
homeassistant/components/tuya/alarm_control_panel.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/binary_sensor.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/button.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/camera.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/climate.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/cover.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/diagnostics.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/event.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/fan.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/humidifier.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/light.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/number.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/scene.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/select.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/sensor.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/siren.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/switch.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/vacuum.py Imports TuyaConfigEntry from coordinator.py.
homeassistant/components/tuya/valve.py Imports TuyaConfigEntry from coordinator.py.

Comment thread homeassistant/components/tuya/coordinator.py
Comment thread homeassistant/components/tuya/coordinator.py
Comment thread homeassistant/components/tuya/coordinator.py
Copilot AI review requested due to automatic review settings April 20, 2026 16:08
Copy link
Copy Markdown
Contributor

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

Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.

Comment thread homeassistant/components/tuya/coordinator.py
Comment thread homeassistant/components/tuya/coordinator.py
Copilot AI review requested due to automatic review settings April 20, 2026 16:44
Copy link
Copy Markdown
Contributor

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

Copilot reviewed 24 out of 24 changed files in this pull request and generated 3 comments.

Comment on lines +18 to 20
from homeassistant.components.tuya.const import DOMAIN
from homeassistant.components.tuya.coordinator import DeviceListener
from homeassistant.const import STATE_UNAVAILABLE
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

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

Align the tests' MockDeviceListener construction with the new DeviceListener constructor signature.

DeviceListener now takes (hass, entry) (and sets manager later), but MockDeviceListener inherits it without overriding __init__ and create_listener/fixtures still call MockDeviceListener(hass, manager), which will raise TypeError when the fixture runs.

Copilot uses AI. Check for mistakes.
Comment on lines +59 to +60
async def async_initialise(self) -> None:
"""Create DeviceListener."""
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

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

Either implement the async_create_listener helper described in the PR description or update the PR description to match the actual approach.

The new module exposes DeviceListener.async_initialise() and does not include an async_create_listener helper, so the PR description currently doesn't reflect the code changes.

Copilot uses AI. Check for mistakes.
Comment on lines +72 to +73
listener = DeviceListener(hass, manager)
manager.add_device_listener(listener)
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

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

Register the same DeviceListener instance with the Tuya Manager instead of constructing a second one with the wrong arguments.

DeviceListener.__init__ expects (hass, entry), but this code calls DeviceListener(hass, manager) which will raise TypeError at runtime; even if corrected, registering a different listener than the one stored in entry.runtime_data would prevent entity updates and make async_unload_entry remove the wrong listener instance.

Suggested change
listener = DeviceListener(hass, manager)
manager.add_device_listener(listener)
manager.add_device_listener(self)

Copilot uses AI. Check for mistakes.
@epenet epenet marked this pull request as draft April 20, 2026 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed code-quality integration: tuya Top 50 Integration is ranked within the top 50 by usage Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants