So I have a campchef grill and I was able to reverse engineer the api and I’d like to integrate with home assistant. I’ve started creating an integration but I can’t seem to get a persistent web socket connection going.
I have the following code being called to connect to the web socket and create a task for the listener to receive messages:
async def async_connect(self):
"""Establish WebSocket connection."""
for key in self.devices:
self.devices[key].session = aiohttp.ClientSession()
try:
# Create a web socket for the given device
self.devices[key].socket = await self.devices[key].session.ws_connect(f"{WEBSOCKET_URL}?path=multiplex")
_LOGGER.debug(f"Connected to {WEBSOCKET_URL}")
self.hass.async_create_task(self.listen_for_updates(self.devices[key]))
except Exception as e:
_LOGGER.error(f"Failed to connect to {WEBSOCKET_URL}: {e}")
Then I have this to send the first message and listen for responses:
async def listen_for_updates(self, device: Device):
_LOGGER.debug(f"Listener initialized for Device: {device._id}")
channel = str(uuid.uuid4())
message = WebSocketMessage(
channel=channel,
path=f"/client-socket/{device._id}",
start=True,
end=False,
queryParams={"jwt": [self.access_token]}
)
json_message = json.dumps(message.__dict__)
# Send a message to the server
await device.socket.send_json(json_message)
_LOGGER.debug(f"WebSocket message sent: {json_message}")
"""Listen for WebSocket messages"""
async for msg in device.socket:
_LOGGER.debug(f"Listener message received: {msg.type} {msg}")
if msg.type == aiohttp.WSMsgType.TEXT:
_LOGGER.debug(f"WebSocket message received: {msg.data}")
response = msg.json()
message = WebSocketMessage.from_json(response)
self.process_message(message)
elif msg.type == aiohttp.WSMsgType.ERROR:
_LOGGER.error(f"WebSocket error: {msg.data}")
But I never get a response back and I’m not quite sure why. I tried creating a socket connection locally via aiohttp in python and I definitely get a response back, so I’m not quite sure what home assistant is doing differently.
Anyone have any examples of a persistent websocket connection in an integration?