Skip to content

Commit

Permalink
Update asterisk_ami.py
Browse files Browse the repository at this point in the history
  • Loading branch information
girlpunk authored Sep 4, 2018
1 parent febc00b commit e95b605
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions asterisk_ami.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
DATA_ASTERISK = 'asterisk'
DATA_MONITOR = 'asterisk-monitor'
DATA_MAILBOX = 'asterisk-mailbox'
DATA_HOST = 'asterisk-host'
DATA_PORT = 'asterisk-port'
DATA_USERNAME = 'asterisk-username'
DATA_PASSWORD = 'asterisk-password'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_HOST): cv.string,
Expand All @@ -40,6 +44,12 @@ def setup(hass, config):
import asterisk.manager
manager = asterisk.manager.Manager()

hass.data[DATA_HOST] = config[DOMAIN].get(CONF_HOST, DEFAULT_HOST)
hass.data[DATA_PORT] = config[DOMAIN].get(CONF_PORT, DEFAULT_PORT)
hass.data[DATA_USERNAME] = config[DOMAIN].get(CONF_USERNAME, DEFAULT_USERNAME)
hass.data[DATA_PASSWORD] = config[DOMAIN].get(CONF_PASSWORD, DEFAULT_PASSWORD)


if not connect(hass):
return False

Expand All @@ -52,14 +62,9 @@ def setup(hass, config):
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")
hass.data[DATA_ASTERISK].connect(hass.data[DATA_HOST], hass.data[DATA_PORT])
login_status = hass.data[DATA_ASTERISK].login(username=hass.data[DATA_USERNAME], secret=hass.data[DATA_PASSWORD]).get_header("Response")
except asterisk.manager.ManagerException as exception:
_LOGGER.error("Error connecting to Asterisk: %s", exception.args[1])
return False
Expand Down

0 comments on commit e95b605

Please sign in to comment.