Xiaomi Gateway Integration

wish I understood well what it was … I don’t know exactly. Permissions on hass folder?

Actually there is a way. Basically you need to spoof dns request to api.ximalaya.com and prepare server which will prepare playlist for your gateway. More details can be found here: https://github.com/andr68rus/miwifiradio.
Also there is a public paid service in Russian Xiaomi community with Russian playlists.

3 Likes

still problems, I added this, this is the log. Hass it blocks, had to run it manually

sorry can you be more clear

this is line 119 of the current file (unchanged), what you want me to do?

def _discover_devices(self):

    cmd = '{"cmd" : "get_id_list"}'
    resp = self._send_cmd(cmd, "get_id_list_ack")
    self.GATEWAY_TOKEN = resp["token"]
    sids = json.loads(resp["data"])

@anon35356645 @rave suggest to add a logging line here.

@rave personally I would like to sponsor you a bit on buying the cube and second gateway.
Maybe other ppl would like to help with a contribution here as well?

Do you have a PayPal address? (sorry don’t own bitcoins anymore ;-))

Thanks a lot! I do have a ‘buy me a beer’ link in the github page in the first post. But i can’t promise I’ll won’t use your donation to buy myself a beer :wink:

Hi @rave and thanks for your work! As you earlier mentioned about interest in getting this merged into homeassistant, I just wanted to point out that it’ll involve extracting the device communication protocol into an external library (looks like it’s already isolated nicely there in components/xiaomi.py :slight_smile: ), and making that as a dependency for the component.

Then the next step will be to create a pull request, and the nice people will help you onwards with the merge & potential code quality issues, although in my opinion the code looks pretty good. One remark though, instead of creating commands as strings with variable placeholders, better use dicts and then json encode just before sending.

Thanks again, I’ll definitely help if/when needed after I receive my set.

edit: https://home-assistant.io/developers/development_submitting/ and other subpages (checklist, testing, …) describe the process.

Thanks for the guide. Quick question though, i see that this is done for most of the components. Is there a reason why the protocol has to be in a different library and hosted somewhere?

I’m honestly not sure how the Xiaomi stuff works, but I do have 2 Gateway’s coming (I ordered parts from 2 different vendors to ensure I could get some stuff soon). This is my first experience with Zigbee, as I’ve been Z-Wave 100% so far (plus some ESP8826 stuff I put together). A starter kit (gateway, PIR, button, door sensor, and plug) should be here tomorrow.

So does the Xiaomi/HASS integration require an internet connection, or is the communication local?

Also, sent you a donation for your hard work so far. The only gap I have with hass is the lack of a Z-Wave (or other) button, that’s relatively cheap. Xiaomi should fill that nicely, though I’d prefer if it’s all local communication (not requiring an internet connection). Keep up the great work, and I hope to see this get implemented with hass in 0.36, or 0.37!

Thanks for the donations! Yes, they communicate through local network connection. So they should still work when the internet connection is down

this is what I get

16-12-30 11:04:10 homeassistant.bootstrap: Setting up xiaomi
16-12-30 11:04:10 custom_components.xiaomi: Discovering Xiaomi Gateways
16-12-30 11:04:11 custom_components.xiaomi: Gateway found on IP 192.168.1.230
16-12-30 11:04:11 custom_components.xiaomi: Creating Multicast Socket
16-12-30 11:04:11 custom_components.xiaomi: Listening
16-12-30 11:04:11 custom_components.xiaomi: Discovering Xiaomi Devices
16-12-30 11:04:11 custom_components.xiaomi: Response does not match return cmd
16-12-30 11:04:11 custom_components.xiaomi: None
16-12-30 11:04:11 homeassistant.bootstrap: Error during setup of component xiaomi
Traceback (most recent call last):
  File "/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant/bootstrap.py", line 151, in _async_setup_component
    None, component.setup, hass, config)
  File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
    value = future.result()
  File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
    raise self._exception
  File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 44, in setup
    XIAOMI_HUB = XiaomiHub(key)
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 113, in __init__
    self._discover_devices()
  File "/home/hass/.homeassistant/custom_components/xiaomi.py", line 120, in _discover_devices
    self.GATEWAY_TOKEN = resp["token"]
TypeError: 'NoneType' object is not subscriptable
16-12-30 11:04:11 homeassistant.core: Bus:Handling <Event call_service[L]: service_data=title=Invalid config, message=The following components and platforms could not be set up:
* [xiaomi](https://home-assistant.io/components/xiaomi/)
Please check your config, notification_id=invalid_config, domain=persistent_notification, service_call_id=1977994192-1, service=create>
16-12-30 11:04:11 homeassistant.core: Bus:Handling <Event state_changed[L]: entity_id=persistent_notification.invalid_config, new_state=<state persistent_notification.invalid_config=The following components and platforms could not be set up:
* [xiaomi](https://home-assistant.io/components/xiaomi/)
Please check your config; title=Invalid config @ 2016-12-30T11:04:11.083200+01:00>, old_state=None>
16-12-30 11:04:11 homeassistant.core: Bus:Handling <Event service_executed[L]: service_call_id=1977994192-1>

Looks like the momentum on all of this is building.

Just a note - I can’t get this working with Wifi at all - it need to be wired into the network to get a response. (worth noting for anyone who is having issues getting it setup)

Does anyone know if the devices act as repeaters? I assumed they wouldn’t as they are all battery powered - but does anyone know if the smart switches that plug into the wall will operate as a repeater? (i.e. you can expand the network with just the use of more smart switches?)

Anyway just an idea… I have a really small house at the moment so the signal reaches end to end from my basic tests thus far.

Place the error logger before the error message

                resp = json.loads(data.decode())
                if resp["cmd"] == rtnCmd:
                    return resp
                else:
                    _LOGGER.error(resp)
                    _LOGGER.error("Response does not match return cmd")

The plug is a repeater according to their website. And according to some people who tested this in Smartthings (i helped to integrate a couple of xiaomi devices into ST), it work as a repeater but some says it doesn’t work. Or maybe it only repeats xiaomi devices

I’m living in a loft, not that big, but i have no problem with Zigbee signal at all from the second floor

Is it possible to control the gateway light using this component? :smiley:

No, unfortunately, i don’t think it is possible. There are no public API for that

I do not understand you. Can you be more clear which is the code I have to insert/replace?

This is what I have now in the file custom_components/xiaomi.py

@anon35356645

At line 166 add _LOGGER.error(resp)

So it becomes:

resp = json.loads(data.decode())
                if resp["cmd"] == rtnCmd:
                    return resp
                else:
                    _LOGGER.error(resp) // Log the response
                    _LOGGER.error("Response does not match return cmd")

Not to be rude, but do you have enough programming experience to debug this yourself?

1 Like

No, I don’t have experience at all. I am able to do copy paste if you tell me where though :smile:
This is what I have in line 166. I copy paste there your code?

I put this

and I receive this