home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
69.88k stars 28.97k forks source link

Bi-Directional Homematic support #1032

Closed danielperna84 closed 8 years ago

danielperna84 commented 8 years ago

Hi, I would like to see bi-directional communication between Home Assistant and Homeatic devices in the future.

Besides the already supported thermostats by Homematic, there are lots of devices that send event-messages to registered logic-providers to perform actions depending on the event-message content. Examples:

There are far more device for which this is true. And as of now, the available thermostat just polls the CCU / Homegear for that information. Which is ok for data with low volatility like temperature. But especially the sensors for doors / windows and switches depend on instant feedback.

That being said, I've put together a (standalone) module that showcases how bi-directional communication for Homematic devices can be implemented. It is a proof of concept which just creates log-messages for the events. For usability I primarily focused on easy interaction with devices, so it's not tailored for integration in HA.

I've had a look at the HA code, but could not grasp on how I could integrate my work in a way that makes sense and suits the architechture of HA. Hence I ask if someone could either guide me on how to integrate my code myself, or rework my code so it can be implemented.

Roughly explained it works like this:

  1. Create a XML RPC server providing certain methods the CCU / Homegear expects and sends eventmessages to (this is my main issue since I don't want to mess up the threading HA is doing)
  2. Create the XML RPC proxy that talks to CCU / Homegear
  3. Handle the devices the CCU / Homegear advertises (e.g. save them for quicker HA startup - essentially this gets us auto-discovery, so later on we would just need to map the device-address to a name to use in HA)
  4. Create the interactive entities (similar to the existing thermostat) which use the already existing proxy-object the server created
  5. Make the XML RPC server pass event-messages to the entities' update()-method so we can send the correct, device-specific information to the HA event-system.

Once bi-directional support is given within HA I would then proceed on implementing further device types (I have access to at least 15 other types of devices besides the 3 existing ones).

You can view my work here: pyhomematic

PS: The thermostat I have implemented would fix the Homematic-specific issue #709, not taking into account HA would generally need support for such modes.

balloob commented 8 years ago

Here are some steps to get started:

jazzaj commented 8 years ago

I would love to join in. Can offer way more motivation than experience. I have a working Homematic XML RPC Server implemented in Java but thats about all. Is there a good example for a "true library" with a similar setup as required for the Homemtaic component in Home Assistant?

danielperna84 commented 8 years ago

I have restructured my module to be a library already since I've created this issue. It's available through pip as well. So actually the only parts missing right now, as far as I can tell, are the proper component and the platforms. Due to lack of time I haven't started with that. Have a look at my referenced repository. The readme as well as the provided example demonstrate how the library can / should be used. All that's needed to get started is installing it via pip and figuring out an efficient implementation.

jazzaj commented 8 years ago

I did some very first steps to integrate the pyhomematic into home-assist (not a working component yet):

https://github.com/jazzaj/home-assistant-homematic/blob/master/homematic.py

I have a good understanding how to use the Entity class to create devices but I have trouble to get a grip on the setup of the devices. Maybe @balloob can give me a hint:

  1. Where should I implement the actual creation of devices?
  2. Do I need to keep track myself on the discovered devices (similar to the TellstickRegistry class in the tellstick implementation?
  3. Am I on the right track with regards to callbacks?

Thanks for any hint...

danielperna84 commented 8 years ago

I had a quick look.

for device in homematic.devices:
    if device._TYPE == "HM-CC-RT-DN":
        # setup Thermostat
    elif ...

As for your questions:

  1. As far as I understand that should be done while iterating over the found devices (the code I've mentioned above). Setting up would create the platform-entities. Those entities need to be able to register their own callbacks so they update themselves as soon as an event occurs.
  2. I'm not sure what you mean. The library detects all devices known by the CCU / Homegear. If new devices get paired, the list of devices get updated. Homeassistant won't notice that unless you add a system-callback (seen in the example.py I believe) which listens for such changes and creates the new entities. Altough I don't think hat's important in the beginning. If new devices get paired, restarting Homeassistant would take care of that.
  3. Don't know about that. At least for stopping everything it looks ok.

My suggestion: Use the library straight from the commandline to get to know how it is behaving. Try to get it working there, and if it runs like that, try to recreate that with the component.

jazzaj commented 8 years ago

Thanks for your comments. I worked through them and made some progress. The code is working:

  1. Server is starting and is auto detecting platforms (not devices). This seems to work via Netdisco (but I'm not sure)
  2. You can manually create HMSwitch devices

Still many things to do:

  1. Only one platform added (Switch)
  2. Addiding more parameters to the configuration of switches (currently it is key only)
  3. Status changes are not yet visible in hass/changes of status does not reach the ccu (issues with pyhomematic)
  4. ...

Guidance regarding auto-detection of devices is highly welcome. Mainly I lack an idea if a) I want this and b) how to trigger the creation of devices

danielperna84 commented 8 years ago

The auto detection of devices is what I am unsure about as well. Since homematic doesn't name its devices, they have to be configured anyways. So the easy way would actually be to just do it like with the already existing thermostat platform, but instead of it creating its own xml rpc proxy, it could use the one provided by pyhomematic. Additionally, the platform would add its update-method as a callback to the corresponding device within pyhomematic. That way the homematic component wouldn't do more than just initializing the local server to receive events, and all the platforms would just leverage the callback-functionality provided by pyhomematic.

jazzaj commented 8 years ago

Meanwhile I have implemented an auto-detection option (will upload soon). Users can use the configuration file to turn this feature on or off. As you have pointed out by default there is no access to the device names and without names the auto detection part seems useless. At least for the Homematic CCU (not sure about Homegear) there is the option to set names but they are not accessible via the XML-RPC interface. The alternative would be for all CCUs that have the XML-API Addon installed (http://www.homematic-inside.de/software/addons/item/xmlapi). With devicelist.cgi you could obtain a list off all device names. What do you think? Do you see an option to integrate this into pyhomematic? Only for CCUs with the add-on installed the names could be added to the devices.

PS: I have left some pull requests and issues in the pyhomematic git... PSS: I'm confident to have by Monday an alpha version to integrate lights, switches and rollershutter into home assisstant.

danielperna84 commented 8 years ago

Since I don't know about XML-API addon for Homegear as well, I suggest to initially focus on manual device/platform configuration. If that's done and working, name-resolving could be a feature within pyhomematic. So you could create the issue for that in my repo, since that's not directly related to home-assistant. I do indeed think that would be nice feature. Although I don't own a CCU, so I'd need some additional information about how all that works. But again, that's part of the pyhomematic-issue, not HA.

I'll have a look at the pull-requests. Thanks for mentioning. Didn't notice that.

balloob commented 8 years ago

Closing this issue as feature requests should be posted in the forum.