python-eq3bt icon indicating copy to clipboard operation
python-eq3bt copied to clipboard

Provide asyncio interface for homeassistant

Open rytilahti opened this issue 1 year ago • 56 comments

It likely doesn't make sense to convert the whole library to use asyncio, but providing a separate homeassistant-friendly interface could be fairly straightforward to implement.

See https://github.com/home-assistant/developers.home-assistant/pull/1412 and https://github.com/home-assistant/developers.home-assistant/pull/1412#issuecomment-1194723457 for best practices.

rytilahti avatar Jul 25 '22 22:07 rytilahti

I just read https://developers.home-assistant.io/docs/network_discovery/ but it is not 100% clear to me what is needed here. Do I understand correctly that the BLEDevice instances need to be passed from homeassistant to the eq3bt library?

TechHummel avatar Aug 08 '22 17:08 TechHummel

@rytilahti Friendly ping!

TechHummel avatar Aug 15 '22 16:08 TechHummel

Hi @TechHummel, sorry for the delay and thanks for the remainder!

Yeah, my understanding is also that the way to handle this would be by passing the BLEDevice created by homeassistant to this library, use it for the update cycle, toss it, and re-do the same procedure on the next cycle. As that doesn't map nicely to the existing API (performing connect on __enter__), it might make sense to create a very lightweight pure-bleak, asyncio interface separate from the existing ones that wraps the synchronous thermostat.

I created previously https://github.com/home-assistant/core/pull/74864 to modernize the code base, but I'm currently occupied with other things so I have no time to look into that until very late autumn or so...

rytilahti avatar Aug 15 '22 16:08 rytilahti

I am looking into this, but I am currently blocked https://github.com/hbldh/bleak/issues/946. Will let you know about any progress.

TechHummel avatar Aug 19 '22 14:08 TechHummel

Hi! I am hitting the same issue here.... I will start checking out this. Any progress on this?

antoniomolram avatar Sep 11 '22 15:09 antoniomolram

Sadly not! Not sure what's causing the issue!

TechHummel avatar Sep 11 '22 15:09 TechHummel

do you have your implementation working on the latest homeassistant? it seems that they want that you don't use the BleakClient() directly, instead the BLEDevice(). I am quite new with the Bleak library too.

It seems that doings so, home assistant can handle multiple BLE python interfaces (what makes sense).

I can use your package as an standalone application (without Home assistant), but once that I integrate it I can't use the functionality, and now I am trying to learn how to debug it :)

antoniomolram avatar Sep 11 '22 16:09 antoniomolram

No, I don't have a working implementation! I am considering just using a MQTT bridge though!

TechHummel avatar Sep 11 '22 17:09 TechHummel

mm, could me maybe a easier solution for the moment, but is quite unfortunate that after a home assistant update the things stopped working (my wife is quite piss off :P)

antoniomolram avatar Sep 13 '22 10:09 antoniomolram

mm, could me maybe a easier solution for the moment, but is quite unfortunate that after a home assistant update the things stopped working (my wife is quite piss off :P)

It's super easy to downgrade HA via the SSH Plugin, worked great for me at least. Tho it's of course only a temporary fix. Make a full backup and download it, before sending this command to your HA.

ha core update --version 2022.6.7

Hope that helps to make your wife a bit happier again. ;-)

cheers, Kay

kaymueller avatar Sep 13 '22 14:09 kaymueller

ha core update --version 2022.6.7

Hope that helps to make your wife a bit happier again. ;-)

I don't have a wife, but thank you very much. I'm happy now. Outside is cold and I need my thermostats working.

batja84 avatar Sep 18 '22 15:09 batja84

I made the downgrade but then other things stop work as I wanted (specially the new zigbee stuff). I will check more in detail all this but time is limited... (wife again) xD

antoniomolram avatar Sep 18 '22 15:09 antoniomolram

I am using Home Assistant now on a downgraded version (2022.6.7) and everything works as it should. However, with every every release I feel the dire consequence of not being able to access the new features and also a bad feeling creeping in due to staying on an outdated version. Our whole apartment works fine with these thermostats and they consume almost no batteries, we recharge once every 18 months. I am more than happy and would love to support any kind of kickstarter request if any of the previous developers would be happy to assigns some valuable time to it.

HandHsince1920 avatar Sep 19 '22 11:09 HandHsince1920

This is no solution, but as a temporary fix, I switched to an bt-mqtt bridge, to work with home-assistant. https://github.com/zewelor/bt-mqtt-gateway

ndurchx avatar Sep 19 '22 12:09 ndurchx

This is no solution, but as a temporary fix, I switched to an bt-mqtt bridge, to work with home-assistant. https://github.com/zewelor/bt-mqtt-gateway

Is there a way to run this on Hassio OS or did you use a separate Pi / PC for hosting this?

wjarka avatar Sep 21 '22 12:09 wjarka

This is no solution, but as a temporary fix, I switched to an bt-mqtt bridge, to work with home-assistant. https://github.com/zewelor/bt-mqtt-gateway

Is there a way to run this on Hassio OS or did you use a separate Pi / PC for hosting this?

I have the same issue and the same question.

LXR85 avatar Sep 21 '22 12:09 LXR85

This is no solution, but as a temporary fix, I switched to an bt-mqtt bridge, to work with home-assistant. https://github.com/zewelor/bt-mqtt-gateway

Is there a way to run this on Hassio OS or did you use a separate Pi / PC for hosting this?

I have had the same issue and the same question.

It is hosted separately. For me it was just another VM, because my HA is also virtualized. It is a bit of a downside, but even if the HA integration is fixed, I will stay at the mqtt bridge. It is much faster, and pops out more usable data, like valve state.

Edit: seems like there is a HA addon version: https://github.com/FaserF/hassio-addons/tree/master/bt-mqtt-gateway

ndurchx avatar Sep 21 '22 12:09 ndurchx

This is no solution, but as a temporary fix, I switched to an bt-mqtt bridge, to work with home-assistant. https://github.com/zewelor/bt-mqtt-gateway

Is there a way to run this on Hassio OS or did you use a separate Pi / PC for hosting this?

I have had the same issue and the same question.

It is hosted separately. For me it was just another VM, because my HA is also virtualized. It is a bit of a downside, but even if the HA integration is fixed, I will stay at the mqtt bridge. It is much faster, and pops out more usable data, like valve state.

Edit: seems like there is a HA addon version: https://github.com/FaserF/hassio-addons/tree/master/bt-mqtt-gateway

What I really liked about the solution via an integration was that it does not create more dependencies in form of further VM's and addons. It came out of the box and worked. 🥇

HandHsince1920 avatar Sep 21 '22 21:09 HandHsince1920

Btw, I was able to get the valve state from the original integration. Can look it up later if you are interested

wjarka avatar Sep 22 '22 07:09 wjarka

like valve state

I can get valve level via template. image image Everything from here can be gotten via templates. image

batja84 avatar Sep 22 '22 15:09 batja84

Hello guys! It there any good changes in 2022.9.7? My HACS integration is dead now "You need HA version 2022.8.0 or newer to use this integration". And I understand further will come more and more problems with obsolete HA Core. It's annoying.

batja84 avatar Oct 02 '22 11:10 batja84

I am using Home Assistant now on a downgraded version (2022.6.7) and everything works as it should. However, with every every release I feel the dire consequence of not being able to access the new features and also a bad feeling creeping in due to staying on an outdated version. Our whole apartment works fine with these thermostats and they consume almost no batteries, we recharge once every 18 months. I am more than happy and would love to support any kind of kickstarter request if any of the previous developers would be happy to assigns some valuable time to it.

Just realised that one can actually support developers directly via github!

HandHsince1920 avatar Oct 05 '22 13:10 HandHsince1920

Hi all,

Meanwhile the issue is not resolved I found a temporal solution with a bluetooth to mqtt gateway. I hope it can help someone. I had mqtt configured previously. But if you don't have mqtt configured you should install the Mosquitto add-on previously. Then do the following:

  1. Set add-on repository on Home Assistant OS to https://github.com/faserf/hassio-addons
  2. Add user bt-mqtt-gateway in Mosquitto add-on or in Home Assistant with a password
  3. Install bt-mqtt-gateway and put the following into the file /share/bt-mqtt-gateway.yaml:
mqtt:
  host: 192.168.1.2
  port: 1883
  username: bt-mqtt-gateway
  password: ***
  client_id: bt-mqtt-gateway
  availability_topic: thermostat/lwt_topic

manager:
  sensor_config:
    topic: homeassistant
    retain: true
  topic_subscription:
    update_all:
      topic: homeassistant/status
      payload: online
  command_timeout: 35
  workers:
    thermostat:
      args:
        devices:
          device1: 00:1A:22:xx:xx:xx
          device2: 00:1A:22:xx:xx:xx
        topic_prefix: thermostat
      topic_subscription: thermostat/+/+/set
      update_interval: 90
  1. Configure the devices with their mac addresses and start the add-on

Thanks to @faserf for the add-on development and to @zewelor for the bluetooth mqtt gateway development.

r-jordan avatar Oct 05 '22 15:10 r-jordan

I've been using the MQTT gateway and it works great. However, since HomeAssistant introduced Bluetooth Proxies, I would love to have EQ3 over BT Proxy. May want to try to debug myself.

wjarka avatar Oct 06 '22 15:10 wjarka

Hi all,

Meanwhile the issue is not resolved I found a temporal solution with a bluetooth to mqtt gateway. I hope it can help someone. I had mqtt configured previously. But if you don't have mqtt configured you should install the Mosquitto add-on previously. Then do the following:

  1. Set add-on repository on Home Assistant OS to https://github.com/faserf/hassio-addons
  2. Add user bt-mqtt-gateway in Mosquitto add-on or in Home Assistant with a password
  3. Install bt-mqtt-gateway and put the following into the file /share/bt-mqtt-gateway.yaml:
mqtt:
  host: 192.168.1.2
  port: 1883
  username: bt-mqtt-gateway
  password: ***
  client_id: bt-mqtt-gateway
  availability_topic: thermostat/lwt_topic

manager:
  sensor_config:
    topic: homeassistant
    retain: true
  topic_subscription:
    update_all:
      topic: homeassistant/status
      payload: online
  command_timeout: 35
  workers:
    thermostat:
      args:
        devices:
          device1: 00:1A:22:xx:xx:xx
          device2: 00:1A:22:xx:xx:xx
        topic_prefix: thermostat
      topic_subscription: thermostat/+/+/set
      update_interval: 90
  1. Configure the devices with their mac addresses and start the add-on

Thanks to @FaserF for the add-on development and to @zewelor for the bluetooth mqtt gateway development.

Stupid question, but where can I find the /share folder?

LXR85 avatar Oct 12 '22 02:10 LXR85

Hi all, Meanwhile the issue is not resolved I found a temporal solution with a bluetooth to mqtt gateway. I hope it can help someone. I had mqtt configured previously. But if you don't have mqtt configured you should install the Mosquitto add-on previously. Then do the following:

  1. Set add-on repository on Home Assistant OS to https://github.com/faserf/hassio-addons
  2. Add user bt-mqtt-gateway in Mosquitto add-on or in Home Assistant with a password
  3. Install bt-mqtt-gateway and put the following into the file /share/bt-mqtt-gateway.yaml:
mqtt:
  host: 192.168.1.2
  port: 1883
  username: bt-mqtt-gateway
  password: ***
  client_id: bt-mqtt-gateway
  availability_topic: thermostat/lwt_topic

manager:
  sensor_config:
    topic: homeassistant
    retain: true
  topic_subscription:
    update_all:
      topic: homeassistant/status
      payload: online
  command_timeout: 35
  workers:
    thermostat:
      args:
        devices:
          device1: 00:1A:22:xx:xx:xx
          device2: 00:1A:22:xx:xx:xx
        topic_prefix: thermostat
      topic_subscription: thermostat/+/+/set
      update_interval: 90
  1. Configure the devices with their mac addresses and start the add-on

Thanks to @FaserF for the add-on development and to @zewelor for the bluetooth mqtt gateway development.

Stupid question, but where can I find the /share folder?

You need to use terminal plugin and use vim / nano to edit the file. At least I didn’t find a way to do it via GUI

wjarka avatar Oct 12 '22 08:10 wjarka

Hi all, As more and more people tend to look at this (broken) integration due to winter coming, I wanted to share my quick alternative to fix this issue without much hassle or custom integrations (MQTT is known to require configuration and altering .py files is also fairly complex). My take on this is simple: use 2 separate HASS instances (onw with latest version - the main one - and another using an older version - the remote one - which still allows the old bluez mechanism) To do this, you should:

  • Install a separate HASS (even HassOS) image
  • Add the eq3btsmart integration to the remote (old version) HASS
  • Install and configure the Remote Home Assistant on both instances (https://github.com/custom-components/remote_homeassistant)
  • Share the Climate devices from the remote HASS to the new HASS

It works, it simply works; the only problem is that you need separate hardware for the remote Hass, as you need BlueZ running on Host. I used an old RPi 3b+ for this and even allowed me to place it in the house so that it reaches via BT the required thermostats.

I consider this approach to be the easiest with almost no impact as you use a separate HW and it's really easy to "undo" once the eq3 (using Bleak) is updated - you just need to remove the auxiliary Remote HASS HW.

Hope this helps. PS: I tried with different approaches before taking this approach (tried altering the eq3 integration to work with bleak - complicated and has many many dependencies; tried writing a custom Py component to work with Eq3 Thermostats - too many flows and use-cases to be handled; also tried replacing the old py files (that work with BlueZ) in the latest HASS codebase - too many errors due to Core dependencies on Bleak) The MQTT approach works well, however I found it to be fairly unstable and I had to constantly debug it

andrei-micu-ro avatar Oct 12 '22 09:10 andrei-micu-ro

I have good news and bad news.

Bad news

I know barely anything about custom integrations, bluetooth, asyncio and python, so there is no way I can send a meaningful PR. Particularly because my changes break CLI usage, and it also requires changes on the Home Assistant's component too.

Good news

I managed to hack something together image

It still has some issues, so I'll try to fix them in the coming days. I hope somebody with more knowledge than me will eventually do it the proper way :) but in the mean time, I will make a temporary custom_component installable through HACS in the next 1 to two weeks.

dbuezas avatar Oct 14 '22 10:10 dbuezas

Here you can find the HACS custom component.

https://github.com/dbuezas/eq3btsmart

I added some extra stuff like:

  • Bluetooth autodiscovery
  • Add via config flow
  • ~~BTProxies support (just using the correct APIs)~~ (probably only in firmware < 1.20)
  • retries
  • ported everything to async
  • fixed setting operation mode
  • allowed to turn off by setting temp to 4.5°
  • fetching schedules in HA
  • (future) setting schedules via HA
image image image

My Apologies to @rytilahti for going this way instead of making a PR, I left my reasons in the readme (mostly that the changes I made were too many, and they break the CLI and other backends)

dbuezas avatar Oct 17 '22 16:10 dbuezas

Hi @dbuezas ,

Thank you!! I have got 3 of these valves and it's working for two of them but it's not connecting to one of the devices and the following errors are showing:

Logger: homeassistant.helpers.entity
Source: custom_components/dbuezas_eq3btsmart/python_eq3bt/eq3bt/bleakconnection.py:97
Integration: dbuezas EQ3 Bluetooth Smart Thermostats
First occurred: 18 d’octubre de 2022, a les 23:14:42 (114 occurrences)
Last logged: 01:30:03

Update for climate.eq3_salon2 fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 515, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 718, in async_device_update
    await task
  File "/config/custom_components/dbuezas_eq3btsmart/climate.py", line 388, in async_update
    await self._async_thermostat_update()
  File "/config/custom_components/dbuezas_eq3btsmart/climate.py", line 381, in _async_thermostat_update
    await self._thermostat.async_update()
  File "/config/custom_components/dbuezas_eq3btsmart/python_eq3bt/eq3bt/eq3btsmart.py", line 239, in async_update
    await self._conn.async_make_request(PROP_WRITE_HANDLE, value)
  File "/config/custom_components/dbuezas_eq3btsmart/python_eq3bt/eq3bt/bleakconnection.py", line 111, in async_make_request
    raise ex
  File "/config/custom_components/dbuezas_eq3btsmart/python_eq3bt/eq3bt/bleakconnection.py", line 97, in async_make_request
    await conn.start_notify(
  File "/usr/local/lib/python3.10/site-packages/bleak/__init__.py", line 546, in start_notify
    raise BleakError(f"Characteristic {char_specifier} not found!")
bleak.exc.BleakError: Characteristic 1056 not found!

I know the device is not the cause because it works ok with the bt-mqtt-gateway.

r-jordan avatar Oct 19 '22 10:10 r-jordan