gluap / pyduofern

GNU General Public License v2.0
40 stars 11 forks source link

trying to get 6way wallswitch type 'ad' to work #6

Open bugfinder opened 5 years ago

bugfinder commented 5 years ago

I have started some work to get this running in my branch of your project https://github.com/bugfinder/pyduofern

apparently all ids some in as lowercase on my system, maybe the items you tested were all numerical up to now.

with my changes and a little debug code I can get the ID of the pushed button, but now I do not have enough knowledge of home-assistant (or actually of python in general ...) to get this working as a usable sensor in HA.

what I get with debugging is: [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster msg: 0f01071a0000010300000000040002ad23116faffe00 [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster code: ad2311 [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster chan: 03 [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster id: 071a [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster chans: ['03'] [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster msg: 0f01071a0000010400000000040002ad23116faffe00 [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster code: ad2311 [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster chan: 04 [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster id: 071a [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster chans: ['04'] [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster msg: 0f01071a0000010500000000040002ad23116faffe00 [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster code: ad2311 [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster chan: 05 [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster id: 071a [/usr/lib/python3.6/site-packages/pyduofern/duofern.py] DUOFERN taster chans: ['05']

so I do get the action "071a" -> "pressed" as "id" and the button number between "01" and "06" as "chan".

how to continue from there ?

gluap commented 5 years ago

@bugfinder sorry for the late reply and thank you for the pointer that the component currently has a problem with case in the hex strings. You are correct: I never had one of the devices with a non-numeric character in the identifier.

The code that creates the components and registers them with homeassistant is in the folder https://github.com/gluap/pyduofern/blob/master/examples/homeassistant/custom_components/duofern/.

The bottom of the file is the class that represents the entity (for instance "cover") and the top does the registration as a device with homeassistant.

For instance for a duofern cover device:

    to_add = [DuofernShutter(device['id'], device['name'], stick, hass) for device in stick.config['devices'] if
              not (device['id'].startswith('46') or device['id'].startswith('43')) and not device['id'] in hass.data[DOMAIN]['devices'].keys()]
    add_devices(to_add)

That line instantiates the DuofernShutter class for each ID that does fulfill the conditions and adds it to HomeAssistant using the add_devices function passed in by homeassistant during initialization.

A good start for developing for homeassistant (if a bit abstract) is the documentation on https://developers.home-assistant.io/docs/en/development_index.html and the example components on https://github.com/home-assistant/example-custom-config/blob/master/custom_components/

A button in homeassistant is a "binary sensor", a simple one as an example would be https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/rpi_gpio/binary_sensor.py.

Even more can be learned by browsing other already-existing homeassistant component source code. For Pyduofern it was a lot of trial and error to get it right and once more when homeassistant changed the component layout.

In your case see several steps to implement this: -> create a class representing one of the buttons of your device (after the model of binary sensor mentioned above) -> for each of the devices of the six-button-type instantiate 6 individual binary sensor classes and add them to homeassistant (basically a modified version of the line above) -> make sure that the "button pressed" state is kept long enough by pyduofern for homeassistant to pick up the state change during the polling. For the current API the duofern component is implemented with, homeassistant polls the component via the update method regularly (every second or so) for what its state is. One way to ensure homeassistant is aware of every button press is to just remember "was pressed" in pyduofern and only clear that state once homeassistant polls.

[It would be more elegant to do an asynchronous implementation where pyduofern actively triggers homeassistant as soon as the button press comes in a callback in and asynchronous api but the current way still uses the old API because pyduofern (as many other components) is not yet ported to asyncio.]

bugfinder commented 5 years ago

Hi,

thanks for your hints, I will try to continue on this. I was on vacation myself and have to find some time to work on this again ... but thanks to you I have some pointers now.