#async socket
1 messages · Page 1 of 1 (latest)
I think asyncio.DatagramProtocol is what you are looking for
This is kind of where the "batteries included" in Python leaves you with dead batteries 🤣
Do you need multicast?
The direct async methods of socket would be these https://docs.python.org/3/library/asyncio-eventloop.html#working-with-socket-objects-directly
where you could use asyncio.get_running_loop() to get the event loop that runs HA
Though streams may be enough for your use case as well, then you don't need to use low level methods. https://docs.python.org/3/library/asyncio-stream.html
I thought about streams, but my grill doesn't really stream, it waits for a command, responds and thats it
as far as I can tell it doesnt send info on any set interval unless you ask it for information
I don't think I do...
it works perfect with my sync code, but I want to make it perfect for home assistant
A stream will just keep the connection open, and you can use dispatcher or eventbus to notifiy when new data has arrived
but new data doesn't arrive unless I ask for it
it almost seems wasteful to keep a connection alive if you don't intend to talk
The point of async is that it supends your task if it is just waiting for data, so it doesn't really block any other processes
data = await reader.read(1024) will wait for data to come in for example.
If you wrap your coroutine in a task like so : asyncio.create_task(tcp_client)
import asyncio
async def tcp_client:
reader, writer = await asyncio.open_connection(
'127.0.0.1', 8888)
data = await reader.read(100)
# notify entities that data is received
asyncio.create_task(tcp_client()) # when running this in HA
asyncio.run(tcp_client()) # when running standalone
Taken from the link i've sent earlier
ok, I'll read up on it after my next appointment, thanks a lot for the info
you could use dispatcher from homeassistant/helpers/dispatcher to notify about data updates within your integration
This is all tcp, do streams work with UDP too?
Good one, it doesn't mention in the docs
There's the example udp echo server: https://docs.python.org/3/library/asyncio-protocol.html#asyncio-udp-echo-server-protocol
It could fit nicely
Anyhow the existing sync code could literally be fully replaced by these methods i believe, just have to use asyncio.get_running_loop() as extra and call the functions from that
In the UDP Echo Client example, I got this talking to my grill, however I'm not sure how the data from datagram_received gets back to asyncio.create_task(tcp_client()). would adding return data pass it back to main(), and if so, where is it getting returned too.
I think the data is being sent back here:transport, protocol = await loop.create_datagram_endpoint(lambda: EchoClientProtocol(message, on_con_lost),remote_addr=('127.0.0.1', 9999)) I'm just not sure how to access it
the data isn't getting returned, you'll need to set up a listener
ugh, async asucks
though what you should probably do to update the data is append your data model as extra parameter to EchoClientProtocol
self.data = datagram_received?