[Custom Component] Roborock communication

Hi everyone,

Just wanted to share my project I have been working on to connect to Roborock and use the latest roborock app in parallel.

Not setup properly with HACS yet, but it can be installed by adding a custom repository in HACS (see link below):

If you are interested in my working out, read the below :slight_smile:

I also wrote up my findings when reverse engineering the communications etc below: -

To get this to work, I had to do something a bit strange, which was to use the tuyapi that has been written, and is maintained with node. With the python port, I just couldn’t get the connection working, and get the messages recognised. So, I developed a node wrapper around the tuyapi, which is used to communicate over file descriptors: -

Then, I wrote a python wrapper around the IPC, so that I could ultimately use this to communicate with the node library easily:

Finally, I then wrote a wrapper around python-miio, that uses tuya, to talk with the device. This was so that all the message parsing etc, was done in the same way as it does with the old miio protocol, because ultimately the message format is the same

Hope that makes sense, and it is of some use to people. Seen other people like me wanting this.

One question for any devs out there. Is there a way to hook into when the hass server is restarting. I will raise an issue on the github too, but the issue is that, the old node process seems to hang around. I just need to be able to close the old session on restart, so any ideas, I am wiling to make the changes!

Any questions or anything else, feel free to reach out to me.

Cheers

Jack

5 Likes

What model Roborock works with this?

I’ve tested on S5 Max, but should work with any that are compatible with the Roborock app. The commands are still the same as MIIO, so if they work with that plugin, they will work here.

Would be useful to see other models…

2 Likes

Thanks, I just tried it on my 0.115.2 running on Docker. From the GUI it says it setup fine, but in the log I see this:

Logger: homeassistant.components.vacuum
Source: custom_components/roborock/vacuum.py:146
Integration: Vacuum (documentation, issues)
First occurred: 14:09:22 (1 occurrences)
Last logged: 14:09:22

Error while setting up roborock platform for vacuum

Traceback (most recent call last): File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 193, in _async_setup_platform await asyncio.shield(task) File “/config/custom_components/roborock/vacuum.py”, line 146, in async_setup_entry tuya_init(js_dir) File “/usr/local/lib/python3.8/site-packages/tuyapipc/init.py”, line 94, in init Popen([‘npm’, ‘install’, ‘tuyapi-ipc’], \ File “/usr/local/lib/python3.8/subprocess.py”, line 854, in init self._execute_child(args, executable, preexec_fn, close_fds, File “/usr/local/lib/python3.8/subprocess.py”, line 1702, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: ‘npm’

so I guess simply that npm, nodejs in general is missing from the official docker container, right?
any way to run these parts outside of HA?

Hey,

Yeh, so it needs Node / npm, to use the tuya-api library (could not get it working with the python equivalent, it is quite out of date).

I assume it would be yeh. However, I did get it running in the local docker environment. Can you connect to your docker installation, and run:

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install nodejs

Looking back on my history within the test env, that is how i installed it. I need to add that to the documentation, that it requires node, completely forgot that people run it in docker ha

Cheers

1 Like

Hi,

had the same idea and just created a custom docker image:

from homeassistant/home-assistant:stable
RUN apk add --update nodejs npm

works like a charm, thanks!
from the first attempt there is now an unavailable entity in HA that I have to find out how to get rid off…

one more thing: your adb instructions don’t work on bluestacks, because run-as does not work.
what I did:

  • install super-su on bluestacks
  • adb shell into it
  • type su to get permissions
  • go to the roborock directory and manually cat the tuya file to /mnt/sdcard/roborock.gz
  • from the host you can then adb pull the gz file

and before I forget: a huge THANK YOU for the component!

1 Like

Nice one, yeh I did some modifications in the APK, so I could just get it via run-as as standard.

When I get a bit more time, will update the README.

No worries, would be good to get someone else using it :slight_smile:

I think I noted it on the Github readme, but one problem I have, is that the node process doesn’t get closed between restarts (this is because it has the same process), need to find a hook to manually close it. For now, I just go on to the system, do “ps -ef | grep ipc”. Kill them and restart, that makes it connect. Otherwise, it just seems to come back unavailable

couldn’t one just restart the container instead of HA?

Yeh, that should work, but more of a workaround right. Would rather have it so, that it just cleans up on restart.

Let me know how you get on or if you are stuck on unknown.

also noticed that in my docker-compose I had to assign a volume to the npm directories, otherwise the component is broken after a restart of the container…obviously.

Makes sense. Might add a step in config flow to install node if it doesn’t exist. Will take a look at that.

Any luck with comms to the device then?

1 Like

Yes works fine! Thanks again for your efforts.

1 Like

from what I see till now the component does not see (at least in the history) what I triggered from the Roborock app, only what I trigger from HA shows up there. Do you see the same?

So, that is interesting, I just checked mine, and it seemed to stop updating. It usually doesn’t update for me only if I don’t kill the old node process on restart.

I have just started the Roborock code in debug mode, so will keep an eye on it, and hopefully see why it was happening. I imagine some crash in the python code during the comms.

If you could do me a favor, and do the same. Change: -

Line 149 - custom_components/roborock/vacuum.py

vacuum = Roborock(ip=host, device_id=device_id, token=token, js_dir=js_dir, debug=True)

To test it is still currently updating, I just update the Do not disturb time by a minute, and see if is reflected in home assistant.

If it stops getting it, and you can send me the logs, would really help, and hopefully get to the bottom of it.

Just to note, the logs won’t appear in the log file, but they get outputted to stdout. On my pi, I just use:

sudo journalctl -u [email protected] --since '5 minutes ago' -f

Not sure how that works in terms of docker. But if not, hopeully I can get to the bottom of it

Will give it a shot in the upcoming days, thanks

A quick reminder, before you restart hass, just run

ps -ef | grep ipc

and kill any tuya processes before restart, hopefully that keeps it in sync

1 Like

so I assume if you get the token via the method listed here, you can not then use the existing Xiaomi Mi Robot Vacuum integration?
any idea what changes when you switch between the xiaomi and roborock apps?

I assume that is the case yeh. I never tried, because I didn’t want to switch over to the Xiaomi app.

I think the whole problem is that when you use the Xiamoi app it communicates with the standard protocol, and it switches to use the tuya protocol on Roborock