Step by step: Asterisk Extension Status

If anyone is interested, I go through my steps of creating a custom component for HA. In this case it is a custom sensor that monitors my voip phones on my Asterisk server. It can be found here.
The project ends up with an automation that flash my Philips Hue lightbulbs when I receive a phone call.

https://www.cron.dk/asterisk-in-home-assistant-part1/
https://www.cron.dk/asterisk-in-home-assistant-part2/
https://www.cron.dk/asterisk-in-home-assistant-part3

Comments are welcome :slight_smile:

Best regards,
Alex

4 Likes

that’s very interesting. Would I be able to use that with grandstream UCM6102 ippbx?. I believed it is based on asterisk

Try to do the telnet test from part one. If you succeed then you can… :grinning:

Best regards
Alex

Hi @crondk,

I have your custom component working and sensor.astext is able to get events for my 3 extensions; however, the extension status on the Home Assistant front end seems to default to “unknown”. If I call an extension, “Ringing” is correctly displayed. Once the call ends, “Idle” is shown for just a brief instant before all the extensions revert back to “unknown” as their status. I’m not sure what exactly is happening or where else to start looking. Any suggestions would be appreciated!

Here is a section of my log showing the important bits:

##### My phone call to test Home Assistant:

Jan 23 09:41:45 home hass[6996]: 2018-01-23 09:41:45 INFO (Thread-3) [custom_components.sensor.astext] Got asterisk event for extension 101: Ringing
Jan 23 09:41:45 home hass[6996]: 2018-01-23 09:41:45 INFO (Thread-3) [custom_components.sensor.astext] Got asterisk event for extension 102: Ringing
Jan 23 09:41:45 home hass[6996]: 2018-01-23 09:41:45 INFO (Thread-3) [custom_components.sensor.astext] Got asterisk event for extension 103: Ringing
Jan 23 09:41:45 home hass[6996]: 2018-01-23 09:41:45 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_101, old_state=<state sensor.asterisk_extension_101=unknown; friendly_name=Asterisk Extension 101 @ 2018-01-23T09:39:28.179585-05:00>, new_state=<state sensor.asterisk_extension_101=Ringing; friendly_name=Asterisk Extension 101 @ 2018-01-23T09:41:45.604166-05:00>>
Jan 23 09:41:45 home hass[6996]: 2018-01-23 09:41:45 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_102, old_state=<state sensor.asterisk_extension_102=unknown; friendly_name=Asterisk Extension 102 @ 2018-01-23T09:39:28.199007-05:00>, new_state=<state sensor.asterisk_extension_102=Ringing; friendly_name=Asterisk Extension 102 @ 2018-01-23T09:41:45.630810-05:00>>
Jan 23 09:41:45 home hass[6996]: 2018-01-23 09:41:45 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_103, old_state=<state sensor.asterisk_extension_103=unknown; friendly_name=Asterisk Extension 103 @ 2018-01-23T09:39:28.157228-05:00>, new_state=<state sensor.asterisk_extension_103=Ringing; friendly_name=Asterisk Extension 103 @ 2018-01-23T09:41:45.636880-05:00>>
Jan 23 09:42:01 home hass[6996]: 2018-01-23 09:42:01 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_103, old_state=<state sensor.asterisk_extension_103=Ringing; friendly_name=Asterisk Extension 103 @ 2018-01-23T09:41:45.636880-05:00>, new_state=<state sensor.asterisk_extension_103=unknown; friendly_name=Asterisk Extension 103 @ 2018-01-23T09:42:01.298801-05:00>>
Jan 23 09:42:01 home hass[6996]: 2018-01-23 09:42:01 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_101, old_state=<state sensor.asterisk_extension_101=Ringing; friendly_name=Asterisk Extension 101 @ 2018-01-23T09:41:45.604166-05:00>, new_state=<state sensor.asterisk_extension_101=unknown; friendly_name=Asterisk Extension 101 @ 2018-01-23T09:42:01.367620-05:00>>
Jan 23 09:42:01 home hass[6996]: 2018-01-23 09:42:01 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_102, old_state=<state sensor.asterisk_extension_102=Ringing; friendly_name=Asterisk Extension 102 @ 2018-01-23T09:41:45.630810-05:00>, new_state=<state sensor.asterisk_extension_102=unknown; friendly_name=Asterisk Extension 102 @ 2018-01-23T09:42:01.488849-05:00>>
Jan 23 09:42:03 home hass[6996]: 2018-01-23 09:42:03 INFO (Thread-3) [custom_components.sensor.astext] Got asterisk event for extension 101: Idle
Jan 23 09:42:03 home hass[6996]: 2018-01-23 09:42:03 INFO (Thread-3) [custom_components.sensor.astext] Got asterisk event for extension 102: Idle
Jan 23 09:42:03 home hass[6996]: 2018-01-23 09:42:03 INFO (Thread-3) [custom_components.sensor.astext] Got asterisk event for extension 103: Idle
Jan 23 09:42:03 home hass[6996]: 2018-01-23 09:42:03 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_101, old_state=<state sensor.asterisk_extension_101=unknown; friendly_name=Asterisk Extension 101 @ 2018-01-23T09:42:01.367620-05:00>, new_state=<state sensor.asterisk_extension_101=Idle; friendly_name=Asterisk Extension 101 @ 2018-01-23T09:42:03.833621-05:00>>
Jan 23 09:42:04 home hass[6996]: 2018-01-23 09:42:04 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_102, old_state=<state sensor.asterisk_extension_102=unknown; friendly_name=Asterisk Extension 102 @ 2018-01-23T09:42:01.488849-05:00>, new_state=<state sensor.asterisk_extension_102=Idle; friendly_name=Asterisk Extension 102 @ 2018-01-23T09:42:04.532485-05:00>>
Jan 23 09:42:04 home hass[6996]: 2018-01-23 09:42:04 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_103, old_state=<state sensor.asterisk_extension_103=unknown; friendly_name=Asterisk Extension 103 @ 2018-01-23T09:42:01.298801-05:00>, new_state=<state sensor.asterisk_extension_103=Idle; friendly_name=Asterisk Extension 103 @ 2018-01-23T09:42:04.537009-05:00>>
Jan 23 09:42:31 home hass[6996]: 2018-01-23 09:42:31 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_103, old_state=<state sensor.asterisk_extension_103=Idle; friendly_name=Asterisk Extension 103 @ 2018-01-23T09:42:04.537009-05:00>, new_state=<state sensor.asterisk_extension_103=unknown; friendly_name=Asterisk Extension 103 @ 2018-01-23T09:42:31.446875-05:00>>
Jan 23 09:42:31 home hass[6996]: 2018-01-23 09:42:31 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_101, old_state=<state sensor.asterisk_extension_101=Idle; friendly_name=Asterisk Extension 101 @ 2018-01-23T09:42:03.833621-05:00>, new_state=<state sensor.asterisk_extension_101=unknown; friendly_name=Asterisk Extension 101 @ 2018-01-23T09:42:31.500716-05:00>>
Jan 23 09:42:31 home hass[6996]: 2018-01-23 09:42:31 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=sensor.asterisk_extension_102, old_state=<state sensor.asterisk_extension_102=Idle; friendly_name=Asterisk Extension 102 @ 2018-01-23T09:42:04.532485-05:00>, new_state=<state sensor.asterisk_extension_102=unknown; friendly_name=Asterisk Extension 102 @ 2018-01-23T09:42:31.559659-05:00>>

I have pasted a more complete portion of the log with things unrelated to asterisk removed here:
https://pastebin.com/SmCPDk98

Here are a few more config items:

configuration.yaml

# Asterisk Component
asterisk:
  host: 10.99.99.201
  port: 5038
  username: !secret
  password: !secret

sensors.yaml

  - platform: astext
    extension: 101
  - platform: astext
    extension: 102
  - platform: astext
    extension: 103

Home Assistant Version: 0.61.0

Regards,
Scott

Hi Alex, I am having difficulties setting up the custom component. I get the error below.

Error loading custom_components.asterisk. Make sure all dependencies are installed
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/loader.py", line 147, in _load_file
module = importlib.import_module(path)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/config/custom_components/asterisk.py", line 1, in <module>
import asterisk.manager
ModuleNotFoundError: No module named 'asterisk'

I have installed pyst2 as stated on part1 and have successfully connected to Freepbx/asterisk using the script from part1 using CLI.

I am clueless and don’t know much about python and linux. Can you or anyone else assist?

Thank
B.

I am trying to make the extension work as well. It seems the custom component architecture changed a bit over the HA version history. I was able to make the component work by creating both asterisk and astext folders under config/custom_components/, creating manifests in each of thoes folders as per custom_components documentation and naming asterisk.py as asterisk/__init__.py and astext.py as astext/sensor.py.

But HA does not always seem to compile them, and when they do work the connection do AMI seems to cut off and does not get reestablished.

Hi Alex J,

I’ve been porting this to the new platform (in my case 0.112.4) and then adding reconnection to the AMI interface.

I’ve also added ability to collect source/destination numbers for both incoming and outgoing calls. Would it be possible to show your the current progress, and talk about collaborating to take it to HACS?

My intent is to provide a card also that shows extension status and incoming/outgoing call details.

Looking forward to discussing this further with you.

All best,
Chris.

I would love to see a working implementation of this. Only so that we can create a status indicator light at everyone’s desk if they are on the phone or not. We use asterisk throughout our office for internal SIP service.

Brian

I am interested in this. Can you share what you did ?

Hello Chris,
is your ported version still operational? If so, could you share it please?
I wouldn’t like to re-port it to the up to date HA if yours is still OK.
Thanks, Peter

there is an add on now that keeps everything easier.