Skip to content

Commit

Permalink
Initial Import
Browse files Browse the repository at this point in the history
  • Loading branch information
girlpunk committed Jul 20, 2017
1 parent b4d7483 commit 3411177
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
56 changes: 56 additions & 0 deletions asterisk_ami.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import logging

import asterisk.manager
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_USERNAME, CONF_PASSWORD
from homeassistant.helpers.discovery import load_platform

DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 5038
DEFAULT_USERNAME = "manager"
DEFAULT_PASSWORD = "manager"
CONF_MONITOR = "monitor"

DOMAIN = "asterisk_ami"
REQUIREMENTS = ['pyst2==0.5.0']
DATA_ASTERISK = 'asterisk'
DATA_MONITOR = 'asterisk-monitor'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT): cv.port,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_MONITOR): cv.ensure_list,
})
}, extra=vol.ALLOW_EXTRA)

_LOGGER = logging.getLogger(__name__)
manager = asterisk.manager.Manager()


def setup(hass, config):
"""Your controller/hub specific code."""

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 e:
_LOGGER.error("Error connecting to Asterisk: %s", e.args[1])
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, [])

load_platform(hass, 'sensor', DOMAIN)

return True
72 changes: 72 additions & 0 deletions sensor/asterisk_ami.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from homeassistant.const import STATE_UNKNOWN
from homeassistant.helpers.entity import Entity

DATA_ASTERISK = 'asterisk'
DATA_MONITOR = 'asterisk-monitor'


def setup_platform(hass, config, add_devices, discovery_info=None):
add_devices([AsteriskSensor(hass)])
for extension in hass.data[DATA_MONITOR]:
add_devices([AsteriskExtension(hass, extension)])


class AsteriskSensor(Entity):
"""Representation of a Sensor."""

def __init__(self, hass):
"""Initialize the sensor."""
self._hass = hass
self._state = STATE_UNKNOWN

@property
def name(self):
"""Return the name of the sensor."""
return 'Asterisk Connection'

@property
def state(self):
"""Return the state of the sensor."""
return self._state

@property
def should_poll(self):
return True

def update(self):
if self._hass.data[DATA_ASTERISK].connected():
self._state = 'connected'
else:
self._state = 'disconnected'


class AsteriskExtension(Entity):
def __init__(self, hass, extension):
self._hass = hass
self._extension = extension
self._state = STATE_UNKNOWN

@property
def name(self):
return "Asterisk Extension " + str(self._extension)

@property
def state(self):
return self._state

@property
def should_poll(self):
return True

def update(self):
response = self._hass.data[DATA_ASTERISK].sipshowpeer(self._extension)
if response.get_header("Response", "Error") == "Error":
self._state = STATE_UNKNOWN
return
else:
status_header = response.get_header("Status", "unknown")
if "OK" in status_header:
self._state = "OK"
return
else:
self._state = status_header

0 comments on commit 3411177

Please sign in to comment.