diyhue / diyHue

Main diyHue software repo
https://diyhue.org/
Other
1.52k stars 274 forks source link

Import "switches" from Home Assistant? #1016

Open jritsema opened 1 month ago

jritsema commented 1 month ago

Feature does not already exist?

I searched and did not find an existing feature request

Summarize feature

Hello. Thank you for this very useful project! When trying to import all of my lights from Home Assistant, I noticed that it only seems to import devices of type "light" and devices of type "switch" are not imported, even though I have set diyhue: include on them in my customize.yaml. Is this intentional behavior? If so, are there any workarounds?

mariusmotea commented 1 month ago

Hi,

Yes, we import just the lights because we can emulate them in Hue lights, but other entities (like switches) we don't have a common Hue device in witch these can be emulated. Probably we can emulate the binary switches in Hue plugs....

Paalap commented 1 month ago

Hi, its possible by adjusting/editing services/homeAssistantWS.py

Original def change_light(self, light, data): service_data = {} service_data['entity_id'] = light.protocol_cfg['entity_id'] payload = { "type": "call_service", "domain": "light", "service_data": service_data } payload["service"] = "turn_on" if 'on' in data: if not data['on']: payload["service"] = "turn_off"

my version def change_light(self, light, data): service_data = {} service_data['entity_id'] = light.protocol_cfg['entity_id'] if light.protocol_cfg['entity_id'].startswith("light."): payload = { "type": "call_service", "domain": "light", "service_data": service_data } elif light.protocol_cfg['entity_id'].startswith("switch."): payload = { "type": "call_service", "domain": "switch", "service_data": service_data } payload["service"] = "turn_on" if 'on' in data: if not data['on']: payload["service"] = "turn_off"

And also from def _should_include(self, ha_state): should_include = False diy_hue_flag = None entity_id = ha_state.get('entity_id', None) if entity_id.startswith("light."):

to def _should_include(self, ha_state): should_include = False diy_hue_flag = None entity_id = ha_state.get('entity_id', None) if entity_id.startswith("light.") or entity_id.startswith("switch."):

This is working fine for me

To make it easier: https://github.com/Paalap/diyHue-1/blob/master/BridgeEmulator/services/homeAssistantWS.py

mariusmotea commented 1 month ago

Hi,

Please create a pull request to master branch with your changes to be more easy to follow.

Paalap commented 1 month ago

Done in https://github.com/diyhue/diyHue/pull/1018

jritsema commented 1 month ago

Wow, this is great! Thank you for the extremely fast turnaround 👍

Just to confirm, I can see that this code change brings switches into diyHue, but has someone verified that diyHue is enable to successfully turn them on/off?

Also, btw, in case it helps others. I got this working using the HASS "Switch as X" Helper, but with this change, I'll be able to remove all of that configuration 🎉