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 config_flow.py, 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 Device.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
else:
    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?

Also interested in this.

I have the project to propose a new integration in homeassistant core for devices that handle cover shutters.
I have a similar need to connect via websocket and implement callbacks for asynchronous server events, so in a cloud_push usage.
After a few weeks of coding, it is correctly working on my local computer. I have still some code to do before creating a PR to the home assistant community but you can review the code here :

This project uses aiohttp and asyncio

The integration implements basic config flow and discovers devices / entities dynamically. The main trick for callbacks is to use the home assistant event bus to propagate the callback data to the device (it updates cover position, disconnection, etc).

I am quite a beginner in homeassistant coding and python and the way I manage to implement it is to search in the homeassistant-core integrations, similar integrations or code to what I want to implement.