Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config flow could not be loaded #2

Open
MRobi1 opened this issue Jul 28, 2023 · 13 comments
Open

Config flow could not be loaded #2

MRobi1 opened this issue Jul 28, 2023 · 13 comments

Comments

@MRobi1
Copy link

MRobi1 commented Jul 28, 2023

Trying to set this up.
v1.0.2 installed via HACS
HA v2023.7.3
OS v10.3

Go to Settings -> Integrations -> + Add Integration -> Select AC Infinity

Giving me Error Config flow could not be loaded: 500 internal server error server got itself in trouble

image

EDIT: Disabling WiFi and going BT Only the controller gets discovered right away and will configure. This error only occurs when trying to manually configure without auto-discovery.

@loganbest
Copy link

Might be important to include the error logs. Here's what shows up for mine when I have wifi and BLE enabled:

Logger: aiohttp.server
Source: custom_components/ac_infinity/config_flow.py:104
Integration: AC Infinity ([documentation](https://github.com/hunterjm/ac-infinity-hacs), [issues](https://github.com/hunterjm/ac-infinity-hacs/issues))
First occurred: August 10, 2023 at 6:38:34 PM (1 occurrences)
Last logged: August 10, 2023 at 6:38:34 PM

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_protocol.py", line 433, in _handle_request
    resp = await request_handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_middlewares.py", line 117, in impl
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 85, in security_filter_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 80, in ban_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 236, in auth_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 148, in handle
    result = await handler(request, **request.match_info)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 149, in post
    return await super().post(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 72, in wrapper
    result = await method(view, request, data, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 71, in post
    result = await self._flow_mgr.async_init(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 856, in async_init
    flow, result = await task
                   ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 884, in _async_init
    result = await self._async_handle_step(flow, flow.init_step, data)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 393, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/ac_infinity/config_flow.py", line 104, in async_step_user
    service_info.advertisement.manufacturer_data[MANUFACTURER_ID]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
KeyError: 2306

@loganbest
Copy link

Did a little digging here and found that it appears the AC Infinity Controller 69 (with wifi) doesn't have it's BLE radio on when wifi is on. When I have wifi on, the discovered devices (in my env) look like this:

2023-08-13 20:06:58.874 INFO (MainThread) [custom_components.ac_infinity.config_flow] user_input: None
2023-08-13 20:06:58.874 INFO (MainThread) [custom_components.ac_infinity.config_flow] self._discovery_info: None
2023-08-13 20:06:58.874 INFO (MainThread) [custom_components.ac_infinity.config_flow] self._discovered_devices: {'<redacted_mac_address>': <home_assistant_bluetooth.models.BluetoothServiceInfoBleak object at 0x7f932f5640>, '<redacted_mac_address>': <home_assistant_bluetooth.models.BluetoothServiceInfoBleak object at 0x7f93141940>, '<redacted_mac_address>': <home_assistant_bluetooth.models.BluetoothServiceInfoBleak object at 0x7f9400d040>, '<redacted_mac_address>': <home_assistant_bluetooth.models.BluetoothServiceInfoBleak object at 0x7f92eba840>}
2023-08-13 20:06:58.874 INFO (MainThread) [custom_components.ac_infinity.config_flow] service_info: {76: b'\x10\x06=\x1d\x86\x02\xe7H'}
2023-08-13 20:06:58.875 INFO (MainThread) [custom_components.ac_infinity.config_flow] discovered device not AC Infinity. This is ok
2023-08-13 20:06:58.875 INFO (MainThread) [custom_components.ac_infinity.config_flow] service_info: {76: b'\x10\x07\x16\x1f\xa5\xafw\x9dx'}
2023-08-13 20:06:58.875 INFO (MainThread) [custom_components.ac_infinity.config_flow] discovered device not AC Infinity. This is ok
2023-08-13 20:06:58.875 INFO (MainThread) [custom_components.ac_infinity.config_flow] service_info: {117: b'B\x04\x01 g!\r\x00\x02\x01F\x01\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x02'}
2023-08-13 20:06:58.875 INFO (MainThread) [custom_components.ac_infinity.config_flow] discovered device not AC Infinity. This is ok
2023-08-13 20:06:58.875 INFO (MainThread) [custom_components.ac_infinity.config_flow] service_info: {1177: b'\x05\x13\x04v:\xc7\x8d\x000\xff\xf0\x04\x14\xb3VJT\x8c\xd4\xb5U\x13)}'}
2023-08-13 20:06:58.875 INFO (MainThread) [custom_components.ac_infinity.config_flow] discovered device not AC Infinity. This is ok
2023-08-13 20:06:58.875 ERROR (MainThread) [custom_components.ac_infinity.config_flow] Could not find any AC Infinity BLE devices

The manufacturer ID of AC Infinity is 2306 which doesn't show any in this list (looking at the service_info lines, key being the ID [76, 117, 1177]).

This was confirmed the second I turned off the Wifi setting from the app, auto-discovery picked it up and spit out these log lines:

2023-08-13 20:16:50.605 INFO (MainThread) [custom_components.ac_infinity.config_flow] DeviceInfo(type=11, name='<redacted_name>', version=7, is_degree=False, tmp_state=0, hum_state=0, vpd_state=0, choose_port=1, tmp=25.08, hum=72.2, vpd=0.88, fan_type=None, fan_state=0, fan=1, work_type=None, level_on=None, level_off=None)
2023-08-13 20:16:50.605 INFO (MainThread) [custom_components.ac_infinity.config_flow] user_input: None
2023-08-13 20:16:50.605 INFO (MainThread) [custom_components.ac_infinity.config_flow] self._discovery_info: <home_assistant_bluetooth.models.BluetoothServiceInfoBleak object at 0x7f8ec2d540>
2023-08-13 20:16:50.605 INFO (MainThread) [custom_components.ac_infinity.config_flow] self._discovered_devices: {'<redacted_mac_address>': <home_assistant_bluetooth.models.BluetoothServiceInfoBleak object at 0x7f8ec2d540>}
2023-08-13 20:16:50.605 INFO (MainThread) [custom_components.ac_infinity.config_flow] service_info: {2306: b'\xf4\x12\xfa\xc3\x84\xe6NRRKN\x07\x0b\x80\t\xcc\x1c4\x01\x01\x00\x00X\x04\x00\x00\x00'}

and now you can see the 2306 ID showing up and auto discovering.

With all of this info I'd say the best option is to consider this as expected behavior and maybe document the caveat since there's nothing we can do to change how AC Infinity turns on the BLE radio.

@Kathou99
Copy link

I have try with your fix @loganbest but I still got this:

Cette erreur provient d'une intégration personnalisée

Logger: custom_components.ac_infinity
Source: custom_components/ac_infinity/coordinator.py:69
Integration: AC Infinity (documentation, issues)
First occurred: 18 août 2023 à 22:16:01 (14 occurrences)
Last logged: 20:59:48

F4:22:12:82:1B:36: Failure while polling
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/ac_infinity_ble/device.py", line 423, in _execute_command_locked
    notify_msg = await self._notify_future
                 ^^^^^^^^^^^^^^^^^^^^^^^^^
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/bluetooth/active_update_coordinator.py", line 129, in _async_poll
    self.data = await self._async_poll_data(self._last_service_info)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/bluetooth/active_update_coordinator.py", line 122, in _async_poll_data
    return await self._poll_method(last_service_info)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/ac_infinity/coordinator.py", line 69, in _async_update
    await self.controller.update()
  File "/usr/local/lib/python3.11/site-packages/ac_infinity_ble/device.py", line 148, in update
    if data := await self._send_command(command):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ac_infinity_ble/device.py", line 367, in _send_command
    return await self._send_command_while_connected(command, retry)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ac_infinity_ble/device.py", line 386, in _send_command_while_connected
    return await self._send_command_locked(command)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/bleak_retry_connector/__init__.py", line 471, in _async_wrap_bluetooth_connection_error_retry
    return await func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ac_infinity_ble/device.py", line 341, in _send_command_locked
    return await self._execute_command_locked(command)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ac_infinity_ble/device.py", line 422, in _execute_command_locked
    async with async_timeout.timeout(5):
  File "/usr/local/lib/python3.11/site-packages/async_timeout/__init__.py", line 129, in __aexit__
    self._do_exit(exc_type)
  File "/usr/local/lib/python3.11/site-packages/async_timeout/__init__.py", line 212, in _do_exit
    raise asyncio.TimeoutError
TimeoutError

@Kathou99
Copy link

and I got this issue too

Cette erreur provient d'une intégration personnalisée

Logger: aiohttp.server
Source: custom_components/ac_infinity/config_flow.py:106
Integration: AC Infinity (documentation, issues)
First occurred: 20:57:45 (5 occurrences)
Last logged: 20:59:42

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_protocol.py", line 433, in _handle_request
    resp = await request_handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_middlewares.py", line 117, in impl
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 85, in security_filter_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 80, in ban_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 236, in auth_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 148, in handle
    result = await handler(request, **request.match_info)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 149, in post
    return await super().post(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 72, in wrapper
    result = await method(view, request, data, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 71, in post
    result = await self._flow_mgr.async_init(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 847, in async_init
    flow, result = await task
                   ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 875, in _async_init
    result = await self._async_handle_step(flow, flow.init_step, data)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 392, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/ac_infinity/config_flow.py", line 106, in async_step_user
    device = parse_manufacturer_data(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ac_infinity_ble/protocol.py", line 58, in parse_manufacturer_data
    tmp=get_short(data, 14) / 100,
        ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ac_infinity_ble/util.py", line 5, in get_short
    return ctypes.c_int16((b[i + 1] & 255) | ((b[i] << 8) & 65280)).value
                           ~^^^^^^^
IndexError: index out of range

@huguesb
Copy link

huguesb commented Oct 14, 2023

I just set up a new fan than comes with the AC 69 Pro controller.
I first had the same issue as #2 (comment)
Then it cleared up and I got the same issue as @Kathou99 above.

Doing some digging with bluetoothctl I found the following:

[bluetooth]# info 34:85:18:67:A8:A2
Device 34:85:18:67:A8:A2 (public)
	Name: G8XDH
	Alias: G8XDH
	Paired: no
	Bonded: no
	Trusted: no
	Blocked: no
	Connected: no
	LegacyPairing: no
	ManufacturerData Key: 0x0902 (2306)
	ManufacturerData Value:
  34 85 18 67 a8 a2 47 38 58 44 48 03 0b 00        4..g..G8XDH...
	ManufacturerData Key: 0x0080 (128)
	ManufacturerData Value:
  80 00 06 00 00 80 00 04 00 00 00                 ...........
	RSSI: 0xffffffa6 (-90)

The manufacturer data field is significantly shorter than expected by ac-infinity-ble!

I couldn't quite figure out what was going on so I installed the AC Infitity android app. It told me to first enable broadcasting by pressing a button until the bluetooth icon blinked on the controller. After doing that connection via bluetooth was successful and I was prompted to update the controller firmware from 1.1.2 to 3.2.25

After completing the firmware upgrade and unpairing the android app, the controller shows up in bluetoothctl with:

Device 34:85:18:67:A8:A2 (public)
	Name: G8XDH
	Alias: G8XDH
	Paired: no
	Bonded: no
	Trusted: no
	Blocked: no
	Connected: no
	LegacyPairing: no
	ManufacturerData Key: 0x0902 (2306)
	ManufacturerData Value:
  34 85 18 67 a8 a2 47 38 58 44 48 07 0b 80 80 00  4..g..G8XDH.....
  80 00 00 00 00 80 00 04 00 00 00                 ...........

Looks like the earlier firmware had a weirdly formed manufacturer data payload...

Now trying again to configure the integration in Home Assistant I'm back to the first error... Fwiw there are a ton of other bluetooth devices in the vicinity. Is it possible that the integration simply needs to filter out any bluetooth device that doesn't have the required manufacturer data?

@huguesb
Copy link

huguesb commented Oct 14, 2023

Update: I added some try/except blocks around the parts that fail to read the manufacturer data and there's definitely something fishy going on:

Oct 13 18:41:50 skynet hass[1693883]: 2023-10-13 18:41:50.262 WARNING (MainThread) [custom_components.ac_infinity.config_flow] <home_assistant_bluetooth.models.BluetoothServiceInfoBleak object at 0x7f90f415bb40>
Oct 13 18:41:50 skynet hass[1693883]: 2023-10-13 18:41:50.262 WARNING (MainThread) [custom_components.ac_infinity.config_flow] AdvertisementData(local_name='G8XDH', manufacturer_data={2306: b'4\x85\x18g\xa8\xa2G8XDH\x07\x0b\x80\x06o\x1c5\x06\x01\x00\x002\x04\x00\x00\x00'}, rssi=-89)
Oct 13 18:41:50 skynet hass[1693883]: 2023-10-13 18:41:50.262 WARNING (MainThread) [custom_components.ac_infinity.config_flow] {2306: b'4\x85\x18g\xa8\xa2G8XDH\x07\x0b\x80\x06o\x1c5\x06\x01\x00\x002\x04\x00\x00\x00'}
Oct 13 18:46:59 skynet hass[1693883]: 2023-10-13 18:46:59.398 ERROR (MainThread) [custom_components.ac_infinity.config_flow] wtf: 2306 AdvertisementData(local_name='Ruuvi 85B7', manufacturer_data={1177: b'\x05\x0e\rZ{\xff\xff\x03\xc0\xfe\\\x00\x10\xad6\xbeZ<\xf4.\x82\xd7\x85\xb7'}, service_uuids=['6e400001-b5a3-f393-e0a9-e50e24dcca9e'], rssi=-91)
Oct 13 18:46:59 skynet hass[1693883]: 2023-10-13 18:46:59.398 ERROR (MainThread) [custom_components.ac_infinity.config_flow] wtf: 2306 AdvertisementData(manufacturer_data={76: b"\x10\x05\x0e\x18'I\xf4"}, tx_power=6, rssi=-92)
Oct 13 18:46:59 skynet hass[1693883]: 2023-10-13 18:46:59.398 ERROR (MainThread) [custom_components.ac_infinity.config_flow] wtf: 2306 AdvertisementData(manufacturer_data={6: b'\x01\x0f \x02H\x01\xbe\x16\x9a@\xde\xc7\xa5\\\xad\x90\xfa\t/J\x8aa\xe5\x98\xab\xaeQ'}, rssi=-90)

Is this a failure to filter out irrelevant advertisements, or some kind of data race wherein the bluetooth advertisement structure is being kept by an async coroutine for later processing and overwritten by some enumeration loop somewhere before the async routines finish their work, resulting in inconsistent view of the advertisement structure and the resulting error?

@huguesb
Copy link

huguesb commented Oct 14, 2023

After adding more logging to the code and restarting HA this particular failure mode is gone. 😕
Possibly it's a side-effect of the controller being too far from my HA instance and the signal being wonky? After moving the two closer to each other (a few meters gets RSSI from -127 to -88) I get more stable signal. Also manually trusting the controller MAC address with bluetoothctl may have helped? Either way, after a reboot the config flow is now hanging without any useful message in the logs... I just get "unknown error occured" in the web UI after a little while

@huguesb
Copy link

huguesb commented Oct 22, 2023

I have finally been successful at pairing my controller and going through the control flow. Turns out I just needed to put the controller back into what the manual calls broadcasting mode so that it would accept pairing. I'm getting sensor reads now and can control the fan!

Thanks for getting this to work! It would be nice to handle those corner cases more gracefully though :)

@dagrah
Copy link

dagrah commented Oct 22, 2023

I´ve been successful too!
If im going to terminal and insert "bluetoothctl scan on", it automatically finds all my AC Infinity Controllers.

@dagrah
Copy link

dagrah commented Oct 24, 2023

Update:
I used a bluetooth stick before and got these problems. If you create a bt proxy with for example esp32, there are no problems anymore.

@huguesb
Copy link

huguesb commented Dec 29, 2023

For what it's worth, I couldn't get things to work reliably, due to a mixture of Bluez being way unreliable on my machine for some reason, the lack of robustness in handling corner cases, and my controller being on a slightly different protocol version

I ended up going a different route: decompiling the newest android app to figure out the changes in protocol, and building a custom BLE->MQTT bridge that works with raw L2CAP sockets. That's been working much better for me: https://github.com/huguesb/ac2mqtt

@MRobi1
Copy link
Author

MRobi1 commented Dec 29, 2023

Were you able to get light control and control for other accessories like clip on fans and humidifiers working?

@huguesb
Copy link

huguesb commented Dec 29, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants