Asterisk in Home Assistant – Part 3

The automation

The final part is to glue things together in an automation. In configuration.yaml i added:

automation:
  - alias: 'Flash lights'
    trigger:
      - platform: state
        entity_id: sensor.asterisk_extension_1000
        to: 'Ringing'
    action:
      - service: light.turn_on
        data:
          entity_id: light.living_room
          flash: long

 

Now calling myself will flash the lighs in my living room πŸ™‚

 

When things go wrong.

I wanted to see what happened if my Asterisk server wasn’t running. It seemed pretty bad for my Home Assistant πŸ™ It took almost 5 minutes to start up!

So I did a little investigation and found out the the pyst2-library wasn’t handling this situation very well. I did a small modification toΒ /srv/homeassistant/lib/python3.5/site-packages/asterisk/manager.pyΒ in the connect() method:

        try:
            _sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            _sock.settimeout(3)
            _sock.connect((host, port))
            _sock.settimeout(None)
            if PY3:

I added a timeout for the socket connect and then set it back to normal after the connection was made. This seemed to solve my problem.

End of project.

I hope you found my little writeup useful. Post a note if you like it or not πŸ™‚

 

 

10 thoughts to “Asterisk in Home Assistant – Part 3”

  1. Just want to say I use Tasker for this.

    Of course you learned way more building all this, and maybe it does more than my solution (to be honest I didn’t read through all the guides because I am not going to use it).

  2. Very interesting!
    I’m an Asterisk/FreePBX user for many years. Long time ago i had some code on my Linux server to announce a callers name or number on my SqueezeBox radio. I’m running HA for a couple of months now and it would be nice to have a callers name or number anounced on my SqueezeBox(es).

    I wil start trying to implement your custom component to just anounce that there is an incoming call.
    If that works i will try to extend it to provide the number and maybe the name later.

    I’m also using Tasker (Automation app for Android) for presence detection etc., but i prefer to do as much as i can from a central place (i.e. HA, Domoticz, Node-Red, MQTT). This to avoid having to implement a solution on all my mobile devices.

    Thanks Alex, for sharing and explaining the details ??

  3. Great post. I admire the effort you have taken to document it and made it interesting to read. I will certaintly give your program a try.

  4. Hi,
    Thanks for starting the work on this custom component for HA asterisk integration. I am running my hassio and ha in docker. I am having an issue trying to get it to find the asterisk.manager module. The module doesn’t seem to be installed even though the REQUIREMENTS = [‘pyst2==0.5.0’] is in the asterisk.py file. Have you tried (or anyone else) running it in hassio docker containers? If you got it working any tips would be appreciated.

    thanks,
    Brendin

    2018-01-24 16:45:39 ERROR (MainThread) [homeassistant.loader] Error loading custom_components.asterisk_ha. Make sure all dependencies are installed
    Traceback (most recent call last):
    File “/usr/lib/python3.6/site-packages/homeassistant/loader.py”, line 142, in get_component
    module = importlib.import_module(path)
    File “/usr/lib/python3.6/importlib/__init__.py”, line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    File “”, line 994, in _gcd_import
    File “”, line 971, in _find_and_load
    File “”, line 955, in _find_and_load_unlocked
    File “”, line 665, in _load_unlocked
    File “”, line 678, in exec_module
    File “”, line 219, in _call_with_frames_removed
    File “/config/custom_components/asterisk_ha.py”, line 1, in
    import asterisk.manager
    ModuleNotFoundError: No module named ‘asterisk’
    2018-01-24 16:45:39 ERROR (MainThread) [homeassistant.loader] Unable to find component asterisk_ha

    1. Hi Brendin,
      I know this is a bit late but the “Custom deps deployment” Hass.io add-on should help. My config says:

      {
      “pypi”: [
      “pyst2”
      ],
      “apk”: []
      }

      Regards,
      Scott

  5. Very nice. I am sure there are all kinds of stuff that can be done. If you had an phone (say extension 2501 off of Asterisk) and when you picked up the phone (connected) then may be send a “pause” to Kodi, DirecTV, Dish Hopper, Plex, Tivo (what ever you had in your living room)? Maybe all of that in the event you maybe watch Tivo and receive a call, 20 minutes later you switch to Hopper and receive another call. Just a thought. One more thought could you take the Caller ID/Name and put it on all the devices in that room? Nice write up, thanks for sharing.

  6. Alex, thanks for the work you put into the Asterisk component. I have a situation where my Asterisk server starts some time after the computer which HASS runs and the component fails with “Could not connect to asterisk server”. Would it be possible to have the component retry connecting to the asterisk server?

    1. Hi David,

      That would most certainly be possible, but then the size of the script would double in size. I wrote it mostly to explain how modules for HA is made. Often error handling needs more code lines than the real functional code πŸ™‚
      You would probably extend the update method in astext.py to try reconnecting if it’s asked for a value. Also line 36-44 in asterisk.py has to be rewritten.

      Best regards,
      Alex

Leave a Reply

Your email address will not be published. Required fields are marked *