We now use await websockets.connect(...), which is asynchronous and non-blocking.
Receive Data: The method recv() is awaited, ensuring that it operates asynchronously.
Sending Data: The method send() is also asynchronous, ensuring it doesn't block the event loop.
Periodic Update Loop: The _periodic_update method is maintained as an infinite loop but now includes error handling for connection issues.
Error Handling: Added error handling in _update_data to manage disconnections and other issues.
Task Cancellation: Ensured that the _periodic_update task is canceled during unload.
The async_step_user method includes error handling to manage connection failures. If the connection cannot be established, an error is displayed on the configuration form.
Utilized Home Assistant's DataUpdateCoordinator to manage data fetching, centralizing updates and reducing redundant code. This approach ensures that data is fetched once and shared among all sensor entities.
Introduced EcostreamSensorBase, a base class for all sensors, which extends CoordinatorEntity. This reduces code duplication and centralizes common functionality.
Disabled polling for each sensor with should_poll = False since the DataUpdateCoordinator handles updates.
Implemented icon and state properties for each sensor type, ensuring that the Home Assistant UI correctly represents the sensor's data.
Sensors are now added via async_add_entities with update_before_add=True, ensuring they start with the latest data.
Open issue:
The async_step_import method handles configurations imported from configuration.yaml. If this is not supported or needed, the method can be further refined or removed.?
WebSocket Connection:
Open issue: The async_step_import method handles configurations imported from configuration.yaml. If this is not supported or needed, the method can be further refined or removed.?