dlarrick / hass-kumo

Home Assistant module interfacing with Mitsubishi mini-split units
MIT License
99 stars 22 forks source link

Possibly error check using arp to detect dhcp change and rewrite cached json #32

Closed omriasta closed 4 years ago

omriasta commented 4 years ago

Just an initial thought since I noticed a few people have issues with blank IP address as well as IP address changes. Theoretically, the KumoCloud servers return the mac address of the unit to us. If the address is blank or old and hasn't been updated on the cloud server, we could possibly use arp | grep "MAC ADDRESS" to retrieve the IP address and write it to the cached json file instead of rewriting the whole json. Not sure if this introduces new issues or what setups might be out there that could cause issues with this, any thoughts? Again, my unit is on a static IP so this doesn't affect me but I believe that the ip is the only part of the json that would change for most people. Let me know if you think this is worthwhile to pursue or if it's a waste?

omriasta commented 4 years ago

I think I would use something like this:

from python_arptable import get_arp_table

mac = "00:00:00:00:00:00"   #   get the mac from the json file
data = get_arp_table()
for device in data:
    if device.get("HW address") == mac:
        print(device.get("IP address"))  # and here we would write this to the json file
dlarrick commented 4 years ago

Not sure if the "password" in the kumo_cache.json (and thus the security token in the unit's API URL) is dependent on the IP address. The thing that sometimes seems to fix missing IP in the config for me (which I see rarely; my IPs are also static) is to open the KumoCloud app, which likely refreshes the service's idea of the addresses.

omriasta commented 4 years ago

I guess I could test that by changing the ip and comparing the json files. I'll take a look when I get a chance...

omriasta commented 4 years ago

OK, here are the very odd results for the following steps...

  1. copied my kumo_cache.json file to make a backup and reference point
  2. changed the unit's ip address
  3. as expected homeassistant could not connect and after a restart homeassistant doesn't finish loading
  4. edited the json file with the new ip and rebooted homeassistant, everything works fine
  5. deleted the integration all together, and then opened the kumo app so that the new ip is saved to kumo cloud
  6. readded the integration and compared the 2 cache files password is the same but token is now different
  7. replaced the json file with the old json file (containing the correct ip but different token), rebooted homeassistant, everything still works (homeassistant boots fine, and controlling the device also works).

Not sure I understand exactly how the token is calculated in pykumo but it seems to depend on serial_crypto and password which both remain constant when the IP changes. If this is the case, we could populate the IP address in the json file dynamically when homeassistant boots using something similar to the above. I could try to look into that but I'm just not sure how I would get the timing correctly so that this all happens before the component reads the json file.

omriasta commented 4 years ago

@dlarrick Since changing the IP address in the kumo cache file seems to work for me regardless of the tokens and the changes in kumo clouds record of the local IP, I have some rough code to get this to work. I will need to make a small addition to pykumo to add a method for the mac address. After that I have a small addition to the climate.py file which will check if the address is in the arp table and use that, otherwise use what kumocloud provides. This way we don't have to change the json file at all.

I am submitting a small PR to pykumo to expose the mac address. Let me know if any issues. Once that's done I can verify all is working with hass-kumo

omriasta commented 4 years ago

Also, this is the code I expect to add to hass-kumo in the climate.py file later on, let me know if any ideas:

arp_table = get_arp_table()
units = account.get_indoor_units()
for unit in units:
    name = account.get_name(unit)
    mac = account.get_mac(unit)
    for device in arp_table:
        if device.get("HW address") == mac:
            address = device.get("IP address")
        else:
            address = account.get_address(unit)
omriasta commented 4 years ago

Thanks @dlarrick for updating pykumo so quickly! I will test my changes for hass-kumo over the weekend and send a PR your way.

omriasta commented 4 years ago

This route is a no go since the arp cache takes quite a while to refresh and this would make the update of the ip address take longer. I thought there may be a chance that homeassistant rebuilds/refreshes the cache when it is restarted but it does not.