Skip to content

Instantly share code, notes, and snippets.

@wwj718
Created June 4, 2018 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wwj718/0cd7619c19b14d6649f61dc5efebd46b to your computer and use it in GitHub Desktop.
Save wwj718/0cd7619c19b14d6649f61dc5efebd46b to your computer and use it in GitHub Desktop.
使用hass.bus.fire和listen
# https://developers.home-assistant.io/docs/en/dev_101_events.html
@wwj718
Copy link
Author

wwj718 commented Jun 4, 2018

@wwj718
Copy link
Author

wwj718 commented Jun 4, 2018

@wwj718
Copy link
Author

wwj718 commented Jun 4, 2018

监听者

/home/pi/.homeassistant/custom_components/hello_listener.py

DOMAIN = 'hello_listener' # 写入configuration.yaml   hello_listener:

def setup(hass, config):
    def handle(event):
        hass.states.set('hello.listener', str(event.data))
        with open("/tmp/listen.txt",'a+') as f:
            # f.write(str(dir(event))+"\r\n")
            f.write(str(event)+"\r\n")
    hass.states.set('hello.listener', 'lll')
    hass.bus.listen('wwj', handle)
    return True

触发者

/home/pi/.homeassistant/python_scripts/hello_world.py

name = data.get('name', 'world')
logger.info("Hello {}".format(name))
hass.bus.fire("wwj", { "wow": "name:{}".format(name) })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment