cmroche / greeclimate

Python package for controlling Gree based minisplit systems
GNU General Public License v3.0
44 stars 22 forks source link

Discovery not working #59

Closed Jc2k closed 2 years ago

Jc2k commented 2 years ago

Possibly a dupe of https://github.com/cmroche/greeclimate/issues/39 but not enough information to be sure.

TL;DR: I think netifaces is returning the wrong broadcast address. If i manually set a broadcast address i can get it to work. It looks like netifaces is unmaintained so I can't report a bug there.

I think there are 2 thngs we can do here:

Here are the notes from my deep dive into this.

From where Home Assistant is running:

$ ip a
...
5: vlan101@if3748: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 56:95:63:ff:1b:65 brd ff:ff:ff:ff:ff:ff
    inet 10.192.170.2/24 scope global vlan101
       valid_lft forever preferred_lft forever
    inet6 fe80::5495:63ff:feff:1b65/64 scope link 
       valid_lft forever preferred_lft forever

A EcoAir rebadged gree unit is on 10.192.170.6. So same VLAN.

First attempt playing around in python shell:

import asyncio
from greeclimate.discovery  import Discovery

>>> async def main():
...     d = Discovery()
...     s = await d.scan(30)
...     print(s)
... 
>>> asyncio.run(main())
asyncio - DEBUG - Using selector: EpollSelector
greeclimate.discovery - INFO - Scanning for Gree devices ...
greeclimate.discovery - DEBUG - Listening for devices on 10.192.170.2 using 10.192.170.2
greeclimate.network - DEBUG - Sending packet:
{"t": "scan"}

[]
>>> 

This bit didn't seem right:

Listening for devices on 10.192.170.2 using 10.192.170.2

import netifaces

for iface in netifaces.interfaces():
    print(iface)
    for addr in netifaces.ifaddresses(iface).get(netifaces.AF_INET, []):
      ipaddr = addr.get("addr")
      bdr = addr.get("broadcast")
      peer = addr.get("peer")
      print(ipaddr)
      print(bdr)
      print(peer)
      print(addr)
      print("---")

Does not match the output of:

ip route show table local dev vlan101 scope link

The first says the incorrect broadcast address, the second says what i expected it to be (10.192.170.255). So lets try that:

import asyncio
from greeclimate.discovery  import Discovery
from greeclimate.network import BroadcastListenerProtocol, IPAddr, IPInterface

async def main():
    d = Discovery()
    s = await d.search_devices([IPInterface("10.192.170.2", "10.192.170.255")])
    await asyncio.sleep(30)
    s = await asyncio.gather(*d.tasks, return_exceptions=True)
    print(d._device_infos)

asyncio.run(main())
asyncio - DEBUG - Using selector: EpollSelector
greeclimate.discovery - DEBUG - Listening for devices on 10.192.170.2 using 10.192.170.255
greeclimate.network - DEBUG - Sending packet:
{"t": "scan"}
greeclimate.network - DEBUG - Received packet from 10.192.170.6:
{"t": "pack", "i": 1, "uid": 0, "cid": "xxxx", "tcid": "", "pack": {"t": "dev", "cid": "xxx", "bc": "000000000000000000000000000000", "brand": "gree", "catalog": "gree", "mac": "xxx", "mid": "13000", "model": "gree", "name": "xxx", "series": "gree", "vender": "1", "ver": "V1.2.1", "lock": 0}}
greeclimate.discovery - INFO - Found gree device Device: xxx @ 10.192.170.6:7000 (mac: xxx)

[<greeclimate.device.DeviceInfo object at 0x7f891d4bad30>]
Jc2k commented 2 years ago

For that to work we'd need to modify this to take a list of broadcast addresses, not a list of IPInterface. I slapped together a quick prototype of this and it seems to work. We'd just need to set local_addr to ("0.0.0.0", 0).

After updating the config flow and _async_scan_update in homeassistant to call async_get_ipv4_broadcast_addresses and pass it to Discovery().scan I now have:

Screenshot 2022-07-13 at 21 49 40

Jc2k commented 2 years ago

Hey @cmroche. Sorry for not following this up sooner, i've been busy with some other Home Assistant features. I'd like to try and get this fixed in the August release of HA. Beta is tomorrow, but this is IMO a bug fix, so we should still be able to do it. If i get a PR together for this and HA, will you be able to do reviews/releases?

cmroche commented 2 years ago

@Jc2k I'll take a look.

cmroche commented 2 years ago

Released 1.3.0

rufik commented 2 years ago

@cmroche I have two wifi networks (VLANs) - first is home network which AC controller is using, and second is for my IoT devices. HA host has two interfaces, to access both of them. HA has configured only second one (for multicast traffic) but I had to "enable" the first one as well to process Gree Integration initial config sucessfully. My question is - once my Gree Integration has been added to HA and it's working, am I allowed to "disable" this first interface? Does all traffic go via unicast now (even when HA is restarted)?