diff --git a/asterisk_ami.py b/asterisk_ami.py new file mode 100644 index 0000000..c9c16b0 --- /dev/null +++ b/asterisk_ami.py @@ -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 diff --git a/sensor/asterisk_ami.py b/sensor/asterisk_ami.py new file mode 100644 index 0000000..fe986e1 --- /dev/null +++ b/sensor/asterisk_ami.py @@ -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