Skip to content

Commit

Permalink
Automatic reconnect (#4)
Browse files Browse the repository at this point in the history
* Update asterisk_ami.py

* Update asterisk_ami.py
  • Loading branch information
girlpunk authored Sep 4, 2018
1 parent 14d4ad1 commit febc00b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
32 changes: 19 additions & 13 deletions asterisk_ami.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,31 @@ def setup(hass, config):
import asterisk.manager
manager = asterisk.manager.Manager()

host = config[DOMAIN].get(CONF_HOST, DEFAULT_HOST)
port = config[DOMAIN].get(CONF_PORT, DEFAULT_PORT)
username = config[DOMAIN].get(CONF_USERNAME, DEFAULT_USERNAME)
password = config[DOMAIN].get(CONF_PASSWORD, DEFAULT_PASSWORD)

try:
manager.connect(host, port)
login_status = manager.login(username=username, secret=password).get_header("Response")
except asterisk.manager.ManagerException as exception:
_LOGGER.error("Error connecting to Asterisk: %s", exception.args[1])
if not connect(hass):
return False

if "Success" not in login_status:
_LOGGER.error("Could not authenticate: %s", login_status)

hass.data[DATA_ASTERISK] = manager
hass.data[DATA_MONITOR] = config[DOMAIN].get(CONF_MONITOR, [])
hass.data[DATA_MAILBOX] = config[DOMAIN].get(CONF_MAILBOX, [])

load_platform(hass, 'sensor', DOMAIN)

return True

def connect(hass):
host = hass.config[DOMAIN].get(CONF_HOST, DEFAULT_HOST)
port = hass.config[DOMAIN].get(CONF_PORT, DEFAULT_PORT)
username = hass.config[DOMAIN].get(CONF_USERNAME, DEFAULT_USERNAME)
password = hass.config[DOMAIN].get(CONF_PASSWORD, DEFAULT_PASSWORD)

try:
hass.data[DATA_ASTERISK].connect(host, port)
login_status = hass.data[DATA_ASTERISK].login(username=username, secret=password).get_header("Response")
except asterisk.manager.ManagerException as exception:
_LOGGER.error("Error connecting to Asterisk: %s", exception.args[1])
return False
if "Success" not in login_status:
_LOGGER.error("Could not authenticate: %s", login_status)
return False
_LOGGER.info("Successfully connected to Asterisk server")
return True
12 changes: 6 additions & 6 deletions sensor/asterisk_ami.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from homeassistant.const import STATE_UNKNOWN
from homeassistant.helpers.entity import Entity

DATA_ASTERISK = 'asterisk'
DATA_MONITOR = 'asterisk-monitor'
DATA_MAILBOX = 'asterisk-mailbox'
from .. import asterisk_ami as ami_platform

_LOGGER = logging.getLogger(__name__)

Expand All @@ -15,9 +13,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
Loads configuration and creates devices for extensions and mailboxes
"""
add_devices([AsteriskSensor(hass)])
for extension in hass.data[DATA_MONITOR]:
for extension in hass.data[ami_platform.DATA_MONITOR]:
add_devices([AsteriskExtension(hass, extension)])
for mailbox in hass.data[DATA_MAILBOX]:
for mailbox in hass.data[ami_platform.DATA_MAILBOX]:
add_devices([AsteriskMailbox(hass, mailbox)])


Expand Down Expand Up @@ -45,11 +43,13 @@ def should_poll(self):

def update(self):
"""Check the connection status and update the internal state"""
if self._hass.data[DATA_ASTERISK].connected():
if self._hass.data[ami_platform.DATA_ASTERISK].connected():
self._state = 'connected'
else:
self._state = 'disconnected'

ami_platform.connect(self._hass)


class AsteriskExtension(Entity):
"""SIP extension connection status (registration) sensor."""
Expand Down

0 comments on commit febc00b

Please sign in to comment.