How to integrate with a permanent cloud_push websocket connection, where to start? Please help

Hello everyone,

I would like to ask for help. I’d like to create a custom integration that uses my API. I know what I would like to achieve, but I do not know how to achieve it. Unfortunately, I don’t know where to start. The integration would be a cloud push.

My API has several methods (the most important).
(method) connect() - to connect to the websocket server and log the user in (handles ConnecionError and AuthError exceptions and restores connection in case of failure)
(method) close() - to close the websocket connection
(method) update_all() - to get all data from websocket service. The method returns a list of Device objects
(method) change_parameter() - to change the parameter

The Device object has the structure:
(method) register_callback (callback) - registers the callback that is called after data update
(method) unregister_callback (callback) - removes a registered callback
(method) update_from_dict (data) - called by API when device data has been updated, registered callbacks are triggered at the end of data processing
(property) device_info - contains information about the device (e.g. device_id, device_name, device_manufacturer, device_software_version)
(property) data: dict [int, [int, Parameter]] - holds device data. The first index is the parameter group, the second index is the parameter number, the data[group_no][param_no] value contains the Parameter list with the structure as below.

Parameter structure:
(property) name - parameter name
(property) value - parameter value
(property) unit - parameter unit (if None = no unit)
(property) min - maximum possible value (if None = parameter cannot be changed)
(property) max - minimum possible value (if None = parameter cannot be changed)

Some parameters are “read-only”, some can be changed by calling the appropriate method from the API (update_parameter(device_id, group_no, param_no)), so I think it would be good to store device_id, group_no, param_no in entity extra_state_attributes

I have already created, it connects to the websocket, check the data for user authentication, in the next flow step I display the possibility of selecting devices that are available for a moment, which user can choose to integrate.
I would like only the devices selected by the user to be added to the integration and that in the event of an empty list of devices (selected by the user, downloaded from websocket service, or when the devices are no longer available on websocket service), the integration would also be created, but that it would only connect to the website and kept connected.
I would like to be able to configure (select) devices from the integration level (async_get_options_flow(config_entry)), but I cannot do it well.
When there are devices selected by the user, to create the appropriate entities according to the data[...].
To choose the platform with which to integrate the parameter, it is probably best to check?

if isinstance(data[group_no][param_no], bool):
    entity_platform = Platform.BINARY_SENSOR
    entity_platform = Platform.SENSOR

I don’t know if I should use DataUpdateCoordinator or not.
I do not know where I should establish and close the connection (the connection should be available from the moment of adding the integration until integration unload)

I tried to use examples from:

But I don’t know how to put it all together into one working integration :slight_smile:
Would anyone be able to help me understand what, how and why and take the right direction? :slight_smile:
Documentation for Home Assistant developers is definitely not enough comprehensive and understandable for me.

Did you solve it?