Move Tuya listener classes to separate module#168636
Conversation
There was a problem hiding this comment.
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.pyto hostDeviceListener,TokenListener, and theTuyaConfigEntrytype alias. - Updated Tuya platform modules to import
TuyaConfigEntryfromcoordinator.pyinstead of__init__.py. - Simplified
entry.runtime_datato store only the listener and adjusted unload logic to access the manager vialistener.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. |
| from homeassistant.components.tuya.const import DOMAIN | ||
| from homeassistant.components.tuya.coordinator import DeviceListener | ||
| from homeassistant.const import STATE_UNAVAILABLE |
There was a problem hiding this comment.
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.
| async def async_initialise(self) -> None: | ||
| """Create DeviceListener.""" |
There was a problem hiding this comment.
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.
| listener = DeviceListener(hass, manager) | ||
| manager.add_device_listener(listener) |
There was a problem hiding this comment.
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.
| listener = DeviceListener(hass, manager) | |
| manager.add_device_listener(listener) | |
| manager.add_device_listener(self) |
Proposed change
DeviceListener,TokenListenerclasses andTuyaConfigEntrytype alias tocoordinator.pyDeviceListener(the manager can be accessed via the corresponding attribute)async_create_listenerhelper to create the TokenListener, the Manager and the DeviceListenerType of change
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: