Can anyone provide me some basic Serial config and write/read samples for developing a custom intgeration? I would like to communicate via Serial port with an Arduino based board. I really would like to know how I can simply write and read to serial port from my custom integration.
Since i used to run my HA on a virtual HyperV machine.
This limited my options, as a serial port wasn’t available. I also didn’t want to end up pulling long cables either.
So I used serial2tcp and i ended up sending/receiving tcp with a EspHome module flashed with JeeLab
Let me clarify a bit what I would like to achieve. I made this RF USB dongle to wirelessly control mechanical home ventilations.
This will be connected to my Raspberry Pi and shows up as a virtual com port. To communicate with this USB dongle, I need to implement serial read and write commands. I would like to make my own custom HA integration for this. I already looked at: Link. But I think here it is not clear how to write to serial port?
At this moment, the USB dongle runs Firmata to communicate with HA, not optimal.
Yes I think that the serial integration is a sensor only, and therefore only reads serial. However it uses pyserial-asyncio so that probably gives a hint.
Firmata is the standard way of serial comms with an arduino type device. If you are having problems (you haven’t said in what way it is “not optimal”) why not post a github issue?
The idea of Firmata is to have control over Arduino’s IO’s in HA, that is not what I want to achieve, I would like to control and read out the actual the fan speed, this is done by sending RF messages with my onboard RF chip and report back to HA via Firmata. Currently I have to use the Firmata “light” entity for this, since I have to have an analog value. Instead I would like to use the FAN entity. Besides it seems that my implementation seems to lose connection after a few days for some reason, cannot find the reason for that. It might be much simpler to implement lightweight response/request communication protocol for sending/requesting FAN speed.
I thought it should be doable to create my own custom integration, since there are other integrations using serial read/write as well.
In the meantime, I know how to send and receive data on the Serial port. What I’m still not sure about, how to implement a simple FAN entity. How can I then see the entity attribute?
Your FanEntity class is not inheriting FanEntity but Entity, therefore your percentage property does not exist on Entity to override. Your class definition should be:
class FanEntity(FanEntity):
"""Representation of a Serial fan."""
....
Hi Marcelh,
could you tell me, how you reach the sending and receiving data on the Serial port?
I have exactly the same issue like you - I want my own integration for my own device, which communicates with UART.
I have started with this example integraion detailed_hello_world_push
and try now to customize this for me with serial communication.
I would be very happy, if you (or someone) could help me!
Thank you for your answer!!
I have copied the 3 files in my custom_components folder, a new folder “serial”.
Added
# Example configuration.yaml entry
sensor:
- platform: serial
serial_port: /dev/serial0
in the configuration.yaml and restart HA.
After I send something via UART, I get the following Log:
Logger: homeassistant.components.serial.sensor
Source: components/serial/sensor.py:202
Integration: serial (documentation, issues)
First occurred: 01:11:55 (1 occurrences)
Last logged: 01:11:55
Error while reading serial device /dev/serial0: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/serial/sensor.py", line 202, in serial_read
line = await reader.readline()
File "/usr/local/lib/python3.10/asyncio/streams.py", line 525, in readline
line = await self.readuntil(sep)
File "/usr/local/lib/python3.10/asyncio/streams.py", line 617, in readuntil
await self._wait_for_data('readuntil')
File "/usr/local/lib/python3.10/asyncio/streams.py", line 502, in _wait_for_data
await self._waiter
File "/usr/local/lib/python3.10/site-packages/serial_asyncio/__init__.py", line 115, in _read_ready
data = self._serial.read(self._max_read_size)
File "/usr/local/lib/python3.10/site-packages/serial/serialposix.py", line 595, in read
raise SerialException(
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
I found out that the port is already used by nodered.
(My “Integration” is currently in nodered and this used the port).
When I stop nodered (also don’t start with boot), the error ’ device reports readiness to read but returned no data’ isn’t there anymore.
Now I try to find a serial-message I send, but I can’t find it…
(I changed the Baudrate in the sensor.py and the rest of the settings should work for me.)
I think the line CONF_SERIAL_PORT = “serial_port” says, that the serial_port: /dev/serial0 from the configuration.yaml should be used - am I correct?
I just noticed that neither the message "Unable to connect to the serial device %s: %s. Will retry"
nor the message "Serial device %s connected"
are in my System Log…