rytilahti / python-eq3bt

Python library and command-line tool for eQ-3 Smart Bluetooth thermostats
MIT License
116 stars 37 forks source link

BleakDBusError: Operation failed with ATT error: 0x0e (Connection Rejected #56

Closed wieluk closed 1 year ago

wieluk commented 2 years ago

Hey there not sure what is happening with the new release 0.2. I tried it because I use eq3 on home assistant.

pi@VpnPi:~ $ eq3cli
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.9/site-packages/eq3bt/bleakconnection.py", line 106, in make_request
    with self:
  File "/home/pi/.local/lib/python3.9/site-packages/eq3bt/bleakconnection.py", line 65, in __enter__
    self._loop.run_until_complete(
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/client.py", line 931, in start_no                                                     tify
    assert_reply(reply)
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/utils.py", line 23, in assert_rep                                                     ly
    raise BleakDBusError(reply.error_name, reply.body)
bleak.exc.BleakDBusError: [org.bluez.Error.Failed] Operation failed with ATT error: 0x0e (Connection Rejected                                                      Due To Security Reasons)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/pi/.local/bin/eq3cli", line 8, in <module>
    sys.exit(cli())
  File "/home/pi/.local/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/pi/.local/lib/python3.9/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/pi/.local/lib/python3.9/site-packages/click/core.py", line 1635, in invoke
    rv = super().invoke(ctx)
  File "/home/pi/.local/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/pi/.local/lib/python3.9/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/pi/.local/lib/python3.9/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/pi/.local/lib/python3.9/site-packages/eq3bt/eq3cli.py", line 47, in cli
    thermostat.update()
  File "/home/pi/.local/lib/python3.9/site-packages/eq3bt/eq3btsmart.py", line 217, in update
    self._conn.make_request(PROP_WRITE_HANDLE, value)
  File "/home/pi/.local/lib/python3.9/site-packages/eq3bt/bleakconnection.py", line 123, in make_request
    raise BackendException("Exception on write using bleak") from ex
eq3bt.BackendException: Exception on write using bleak

Anyone knows what is happening here?

rytilahti commented 2 years ago

Did it work previously / does it work with other backends? You can try to use --backend option to select either bluepy or gattlib to see if that helps.

If it didn't work previously, maybe your thermostat is running a firmware that requires pairing like discussed in the readme?

wieluk commented 2 years ago

I thing I paired my devices but it is long ago so I am not 100% sure. but with bluepy it works. Do I have to install bleak separately?

pi@VpnPi:~ $ eq3cli --mac ... --backend bluepy
[...] Target 16.0 (mode: manual (16.0C) dst, away: no)
Locked: False
Batter low: False
Window open: False
Window open temp: 5.0
Window open time: 0:00:00
Boost: False
Current target temp: 16.0
Current comfort temp: 22.0
Current eco temp: 17.0
Current temp offset: 1.5
Current mode: manual (16.0C) dst
Valve: 0

gattlib also works

rytilahti commented 2 years ago

Huh, that's odd... You have bleak installed as the stacktrace contains hints to it, so there should be nothing else to do. That error message implies that the device is not accepting the connection, but I don't really know what could cause that especially if it's working with other backends...

I'll pin this issue just in case it catches someone's eye, it would be great to find a solution for this if it's not a problem in your setup.

toalex77 commented 2 years ago

I just found the same (or similar) issue.

I've installed latest version of python-eq3, because of homeassistant, as OP, and to test if I can try to upgrade to latest version the rest of my home assistant setup.

When I test it, I obtain similar output, but, with --debug option I get differents output on different valves.

This is the output of a valve that does not work with any backend (so can be a pairing issue):

eq3cli --debug --backend bleak --mac XX:XX:XX:XX:XX:XX state
DEBUG:asyncio:Using selector: EpollSelector
DEBUG:asyncio:Using selector: EpollSelector
DEBUG:eq3bt.eq3btsmart:Querying the device..
DEBUG:eq3bt.bleakconnection:Trying to connect to XX:XX:XX:XX:XX:XX
DEBUG:eq3bt.bleakconnection:Connected to XX:XX:XX:XX:XX:XX
DEBUG:eq3bt.bleakconnection:Writing b'03160713123800' to 1041
DEBUG:eq3bt.bleakconnection:Waiting for notifications for 1
Traceback (most recent call last):
  File "/opt/homeassistant/venv_3.9.0/bin/eq3cli", line 8, in <module>
    sys.exit(cli())
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/core.py", line 1256, in invoke
    Command.invoke(self, ctx)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/eq3bt/eq3cli.py", line 47, in cli
    thermostat.update()
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/eq3bt/eq3btsmart.py", line 217, in update
    self._conn.make_request(PROP_WRITE_HANDLE, value)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/eq3bt/bleakconnection.py", line 119, in make_request
    self._loop.run_until_complete(self.wait_for_response(timeout))
  File "/opt/python-3.9.0/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/eq3bt/bleakconnection.py", line 101, in wait_for_response
    await asyncio.wait_for(self._notifyevent.wait(), timeout)
  File "/opt/python-3.9.0/lib/python3.9/asyncio/tasks.py", line 476, in wait_for
    return fut.result()
  File "/opt/python-3.9.0/lib/python3.9/asyncio/locks.py", line 226, in wait
    await fut
RuntimeError: Task <Task pending name='Task-9' coro=<Event.wait() running at /opt/python-3.9.0/lib/python3.9/asyncio/locks.py:226> cb=[_release_waiter(<Future pendi...x757950a0>()]>)() at /opt/python-3.9.0/lib/python3.9/asyncio/tasks.py:414]> got Future <Future pending> attached to a different loop

And this is the output of a valve that instead works with other backends:

eq3cli --debug --backend bleak --mac YY:YY:YY:YY:YY:YY state
DEBUG:asyncio:Using selector: EpollSelector
DEBUG:asyncio:Using selector: EpollSelector
DEBUG:eq3bt.eq3btsmart:Querying the device..
DEBUG:eq3bt.bleakconnection:Trying to connect to YY:YY:YY:YY:YY:YY
DEBUG:eq3bt.bleakconnection:Connected to YY:YY:YY:YY:YY:YY
DEBUG:eq3bt.bleakconnection:Writing b'03160713123823' to 1041
DEBUG:eq3bt.bleakconnection:Waiting for notifications for 1
DEBUG:eq3bt.bleakconnection:Got notification from 1057: b'020129000409000000001c06282207'
DEBUG:eq3bt.eq3btsmart:Received notification from the device..
DEBUG:eq3bt.eq3btsmart:Got status: b'020129000409000000001c06282207'
DEBUG:eq3bt.eq3btsmart:Parsed status: Container: 
    cmd = 2
    mode = Container: 
        AUTO = True
        MANUAL = True
        DST = True
        LOCKED = True
    valve = 0
    target_temp = 4.5
    away = b'\x00\x00\x00\x00' (total 4)
    presets = Container: 
        window_open_temp = 14.0
        window_open_time = 0:30:00
        comfort_temp = 20.0
        eco_temp = 17.0
        offset = 0.0
DEBUG:eq3bt.eq3btsmart:Valve state:      0
DEBUG:eq3bt.eq3btsmart:Mode:             manual off dst locked
DEBUG:eq3bt.eq3btsmart:Target temp:      4.5
DEBUG:eq3bt.eq3btsmart:Away end:         None
DEBUG:eq3bt.eq3btsmart:Window open temp: 14.0
DEBUG:eq3bt.eq3btsmart:Window open time: 0:30:00
DEBUG:eq3bt.eq3btsmart:Comfort temp:     20.0
DEBUG:eq3bt.eq3btsmart:Eco temp:         17.0
DEBUG:eq3bt.eq3btsmart:Temp offset:      0.0
Traceback (most recent call last):
  File "/opt/homeassistant/venv_3.9.0/bin/eq3cli", line 8, in <module>
    sys.exit(cli())
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/core.py", line 1256, in invoke
    Command.invoke(self, ctx)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/eq3bt/eq3cli.py", line 47, in cli
    thermostat.update()
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/eq3bt/eq3btsmart.py", line 217, in update
    self._conn.make_request(PROP_WRITE_HANDLE, value)
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/eq3bt/bleakconnection.py", line 119, in make_request
    self._loop.run_until_complete(self.wait_for_response(timeout))
  File "/opt/python-3.9.0/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/opt/homeassistant/venv_3.9.0/lib/python3.9/site-packages/eq3bt/bleakconnection.py", line 101, in wait_for_response
    await asyncio.wait_for(self._notifyevent.wait(), timeout)
  File "/opt/python-3.9.0/lib/python3.9/asyncio/tasks.py", line 476, in wait_for
    return fut.result()
  File "/opt/python-3.9.0/lib/python3.9/asyncio/locks.py", line 226, in wait
    await fut
RuntimeError: Task <Task pending name='Task-9' coro=<Event.wait() running at /opt/python-3.9.0/lib/python3.9/asyncio/locks.py:226> cb=[_release_waiter(<Future pendi...x7580b6a0>()]>)() at /opt/python-3.9.0/lib/python3.9/asyncio/tasks.py:414]> got Future <Future pending> attached to a different loop
rytilahti commented 2 years ago

@toalex77 that seems to be a separate issue, see #57

wieluk commented 2 years ago

Does anyone know what I can try? the bleak-lescan command does work. But I want to use the eq3bt in homeassistant and unfortunately there only bleak works for now...

in the homeassistant container the error looks like this:

bash-5.1# eq3cli --mac <mymac>
Traceback (most recent call last):
  File "/usr/local/bin/eq3cli", line 8, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1635, in invoke
    rv = super().invoke(ctx)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/eq3bt/eq3cli.py", line 47, in cli
    thermostat.update()
  File "/usr/local/lib/python3.10/site-packages/eq3bt/eq3btsmart.py", line 217, in update
    self._conn.make_request(PROP_WRITE_HANDLE, value)
  File "/usr/local/lib/python3.10/site-packages/eq3bt/bleakconnection.py", line 106, in make_request
    with self:
  File "/usr/local/lib/python3.10/site-packages/eq3bt/bleakconnection.py", line 51, in __enter__
    self._loop.run_until_complete(self._conn.connect())
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.10/site-packages/bleak/backends/bluezdbus/client.py", line 107, in connect
    device = await BleakScannerBlueZDBus.find_device_by_address(
  File "/usr/local/lib/python3.10/site-packages/bleak/backends/scanner.py", line 221, in find_device_by_address
    return await cls.find_device_by_filter(
  File "/usr/local/lib/python3.10/site-packages/bleak/backends/scanner.py", line 250, in find_device_by_filter
    async with cls(detection_callback=apply_filter, **kwargs):
  File "/usr/local/lib/python3.10/site-packages/bleak/backends/scanner.py", line 96, in __aenter__
    await self.start()
  File "/usr/local/lib/python3.10/site-packages/bleak/backends/bluezdbus/scanner.py", line 128, in start
    manager = await get_global_bluez_manager()
  File "/usr/local/lib/python3.10/site-packages/bleak/backends/bluezdbus/manager.py", line 812, in get_global_bluez_manager
    await instance.async_init()
  File "/usr/local/lib/python3.10/site-packages/bleak/backends/bluezdbus/manager.py", line 294, in async_init
    bus = MessageBus(bus_type=BusType.SYSTEM)
  File "/usr/local/lib/python3.10/site-packages/dbus_next/aio/message_bus.py", line 122, in __init__
    super().__init__(bus_address, bus_type, ProxyObject)
  File "/usr/local/lib/python3.10/site-packages/dbus_next/message_bus.py", line 85, in __init__
    self._setup_socket()
  File "/usr/local/lib/python3.10/site-packages/dbus_next/message_bus.py", line 575, in _setup_socket
    raise err
  File "/usr/local/lib/python3.10/site-packages/dbus_next/message_bus.py", line 548, in _setup_socket
    self._sock.connect(filename)
FileNotFoundError: [Errno 2] No such file or directory

fixed this by mounting - /var/run/dbus/:/var/run/dbus/ in my docker container. Now I get the same error as in the first post in my container.

Also the --mac address does not matter same error even for randomly selected adresses.

TechHummel commented 2 years ago

I have the same issue now 😢 @wieluk Which version of bluez are you using?

dpkg --status bluez | grep '^Version:'
Version: 5.55-3.1
wieluk commented 2 years ago

Version: 5.55-3.1+rpt1 This is my version. I would like to get this back to working without downgrading homeassistant before heating season anything I can do?

TechHummel commented 2 years ago

Thanks! Maybe @rytilahti is using a different version?

You could setup a MQTT Bridge for bluetooth!

rytilahti commented 2 years ago

I cannot recall if I was doing testing on my laptop (bluez 5.65-2) or on my raspbi (5.55-3.1+rpt1), but the "Connection rejected" error sounds like a permission issue. Unfortunately there are also other issues that relate to homeassistant support, so fixing the permission problem will not fix the underlying problem.

For the time being I'd also suggest using a mqtt bridge. I'm not currently able to find time to fix this issue properly, so in the event no one comes up with a proper solution it may take some time to get this fixed. I'm sorry for that :-(

TechHummel commented 2 years ago

Thanks for your answer! It's actually a wrong error message (or see here).

I will try to test with 5.65. I was hoping I could support on this but it's difficult since I can't get it to work with bleak at all :(