hthiery / python-fritzhome

Python Library to access AVM Fritz!Box homeautomation
MIT License
52 stars 37 forks source link

Several HAN-FUN devices are not correctly identified #80

Closed patgrosse closed 1 year ago

patgrosse commented 1 year ago

I connected two Telekom HAN-FUN devices to my FRITZ!Box 7590 and they did not appear in Home Assistant. I did some research and I feel like the function bits provided by the AHA HTTP Interface are not interpreted the correct way by pyfritzhome.

Devices: Telekom E27 warm white bulb (no white spectrum, just dimmable) https://www.smarthome.de/geraete/smarthome-wlan-led-lampe-e27-weiss Telekom Smart Plug Outdoor https://www.smarthome.de/geraete/smarthome-zwischenstecker-aussen-schwarz

API discussion: If I read your code correctly, I think the interpretation of the following function bits is not correct: 0x0200 aka SWITCH => just the AVM plugs (eg. 200, 210) 0x8000 aka SWITCHABLE => onoff available, meaning the unit accepts "on/off" commands 0x10000 aka DIMMABLE => level control available, meaning the unit accepts "set level to x" commands 0x20000 aka LIGHTBULB => color control available, meaning at least one type of color can be set (temperature or hue/saturation), identified by the bitmap in supported_modes

This means a lightbulb is only identified as lightbulb if it has color control (white spectrum, hue/saturation, ...) and a plug works only for FRITZ!DECT 200 and 210.

To correctly identify a HAN-FUN device one could use the HAN-FUN bit (0x2000) and then check etsiunitinfo->unittype from the XML. It contains the actual device type according to the AHA HTTP Interface. Eg.:

...
262 = AC_OUTLET
263 = AC_OUTLET_SIMPLE_POWER_METERING
...

AVM devices are apparently no HAN-FUN devices (except the DECT 500) and can be identified via the other function bits.

Some debug infos:

fritzhome list output:

##############################
name=Telekom White Dimmable Bulb
  ain=12701 0071939
  id=407
  productname=HAN-FUN
  manufacturer=0x319d
  present=True
  lock=None
  devicelock=None
##############################
name=Telekom White Dimmable Bulb
  ain=12701 0071939-1
  id=2009
  productname=HAN-FUN
  manufacturer=0x319d
  present=True
  lock=None
  devicelock=None
##############################
name=Telekom Plug
  ain=11324 0717699
  id=408
  productname=HAN-FUN
  manufacturer=0x2c3c
  present=True
  lock=None
  devicelock=None
##############################
name=Telekom Plug
  ain=11324 0717699-1
  id=2010
  productname=HAN-FUN
  manufacturer=0x2c3c
  present=True
  lock=None
  devicelock=None

fritzhome device stats output: <devicestats></devicestats>

fritzhome switch on/off: works correctly

Output of homeautoswitch.lua: homeautoswitch_output.xml.txt

MattBavaria commented 1 year ago

Hello Patgrosse,

your proposal would IMHO lead to a complete new and more complex way of identifying devices. That might not even be neccessary. I just opened a pull request to add support for the Magenta power outlets for HomeAssistant. These report themselves as "HAN-FUN" and "SWITCHABLE" (40960 = xA000). At least switching on your lightbulb should work as well, as it reports as "Lightbulb", "HAN-FUN", "SWITCHABLE" and "DIMMABLE" (x1A004).

Regards MattBavaria

patgrosse commented 1 year ago

You're right that this would mean a different (and more complex, yes) way of identifying devices.

Other projects are apparently using this kind of identifying devices in some way: https://gitlab.com/fb-smart-home/fbsmarthome-lambda/-/blob/develop/FBSmartHome/FritzBox.cs#L599 (Alexa and Google Assistant show both the bulb and the plug correctly)

A possibly easier solution (also partly from FBSmartHome) that might have some limitations:

The AHA HTTP interface documentation states:

Bit 2: Licht/Lampe Bit 15: an-/ausschaltbares Gerät/Steckdose/Lampe/Aktor Bit 16: Gerät mit einstellbarem Dimm-, Höhen- bzw. Niveau-Level Bit 17: Lampe mit einstellbarer Farbe/Farbtemperatur

Currently bit 17 is used to identify a lighting device while FBSmartHome uses bit 2 to identify a lighting device. Therefore I would suggest using the following mechanism: bit 2 is set => it's a lighting device + bit 15 is set => on/off available (should always be present, also for a plug) + bit 16 is set => light can be dimmed (also level for blinds) + bit 17 is set => some kind of color can be set (hue saturation or color temperature, obviously only for lighting devices) This way only the functionbitmask has to be evaluated.

MattBavaria commented 1 year ago

You're right that this would mean a different (and more complex, yes) way of identifying devices.

Other projects are apparently using this kind of identifying devices in some way: https://gitlab.com/fb-smart-home/fbsmarthome-lambda/-/blob/develop/FBSmartHome/FritzBox.cs#L599 (Alexa and Google Assistant show both the bulb and the plug correctly)

As far as I understand that code from a quick view, they use that for identifying blinds only. That‘s done within FritzHome with functionbitmask, too.

A possibly easier solution (also partly from FBSmartHome) that might have some limitations:

The AHA HTTP interface documentation states:

Bit 2: Licht/Lampe Bit 15: an-/ausschaltbares Gerät/Steckdose/Lampe/Aktor Bit 16: Gerät mit einstellbarem Dimm-, Höhen- bzw. Niveau-Level Bit 17: Lampe mit einstellbarer Farbe/Farbtemperatur

Currently bit 17 is used to identify a lighting device while FBSmartHome uses bit 2 to identify a lighting device. Therefore I would suggest using the following mechanism: bit 2 is set => it's a lighting device + bit 15 is set => on/off available (should always be present, also for a plug) + bit 16 is set => light can be dimmed (also level for blinds) + bit 17 is set => some kind of color can be set (hue saturation or color temperature, obviously only for lighting devices) This way only the functionbitmask has to be evaluated.

Would support that approach. This could also lead to a more clean code. F.e. I had to reuse some parts already implemented for lightbulb to get my „simpleonoff“ power outlets working. But I did not completely understand, how device identification works exactly within HomeAssistant, because the code of FritzHome is kind of „wrapped“ again.

MattBavaria commented 1 year ago

Forgot to mention: Especially AVM devices seem to show a kind of mixture in class declaration (see DECT200 as example). I would support an overview, which end-consumer product (f.e. AVM DECT200 oder Magenta power outlet ) reports which bits in functionbitmask. If such a lists exists, at least I haven‘t found ist yet.

parliament119 commented 1 year ago

Hey @patgrosse ! I opened up a Pull Request that should fix the problems with detecting Telekom devices: Fix Telekom HANFUN Devices

Maybe you can try it out for yourself. It doesn't fix the lighbulb problem within Home Assistant, as they expect bulbs which always have color-capabilities, but at least pyfritzhome now correctly detects them. Once pyfritzhome is updated we could tackle the problem with Home Assistant.

MattBavaria commented 1 year ago

Hello @parliament119,

I do not understand, why your modification works with Magenta switches, although you didn‘t modify the „set_switch_state“ methods to support the „simpleon/off“ calls directly. Can you please explain briefly ?

Regards, MattBavaria

patgrosse commented 1 year ago

Awesome, thanks @parliament119. The fix works for me.