2018-01-18 22:04:18 +00:00
|
|
|
"""
|
|
|
|
Support for Asterisk Voicemail interface.
|
2017-08-06 18:19:47 +00:00
|
|
|
|
2018-01-18 22:04:18 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/asterisk_mbox/
|
|
|
|
"""
|
2017-08-06 18:19:47 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-01-18 22:04:18 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT
|
2017-08-06 18:19:47 +00:00
|
|
|
from homeassistant.core import callback
|
2018-01-18 22:04:18 +00:00
|
|
|
from homeassistant.helpers import discovery
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.dispatcher import (
|
|
|
|
async_dispatcher_connect, async_dispatcher_send)
|
2017-08-06 18:19:47 +00:00
|
|
|
|
2018-08-30 16:44:37 +00:00
|
|
|
REQUIREMENTS = ['asterisk_mbox==0.5.0']
|
2017-08-06 18:19:47 +00:00
|
|
|
|
2018-01-18 22:04:18 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
DOMAIN = 'asterisk_mbox'
|
|
|
|
|
2018-01-18 22:04:18 +00:00
|
|
|
SIGNAL_MESSAGE_REQUEST = 'asterisk_mbox.message_request'
|
|
|
|
SIGNAL_MESSAGE_UPDATE = 'asterisk_mbox.message_updated'
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
2018-01-18 22:04:18 +00:00
|
|
|
vol.Required(CONF_PORT): int,
|
2017-08-06 18:19:47 +00:00
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
"""Set up for the Asterisk Voicemail box."""
|
|
|
|
conf = config.get(DOMAIN)
|
|
|
|
|
|
|
|
host = conf.get(CONF_HOST)
|
|
|
|
port = conf.get(CONF_PORT)
|
|
|
|
password = conf.get(CONF_PASSWORD)
|
|
|
|
|
|
|
|
hass.data[DOMAIN] = AsteriskData(hass, host, port, password)
|
|
|
|
|
2018-01-18 22:04:18 +00:00
|
|
|
discovery.load_platform(hass, 'mailbox', DOMAIN, {}, config)
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class AsteriskData:
|
2017-08-06 18:19:47 +00:00
|
|
|
"""Store Asterisk mailbox data."""
|
|
|
|
|
|
|
|
def __init__(self, hass, host, port, password):
|
|
|
|
"""Init the Asterisk data object."""
|
|
|
|
from asterisk_mbox import Client as asteriskClient
|
|
|
|
|
|
|
|
self.hass = hass
|
|
|
|
self.client = asteriskClient(host, port, password, self.handle_data)
|
|
|
|
self.messages = []
|
|
|
|
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_MESSAGE_REQUEST, self._request_messages)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def handle_data(self, command, msg):
|
|
|
|
"""Handle changes to the mailbox."""
|
|
|
|
from asterisk_mbox.commands import CMD_MESSAGE_LIST
|
|
|
|
|
|
|
|
if command == CMD_MESSAGE_LIST:
|
2018-01-18 22:04:18 +00:00
|
|
|
_LOGGER.debug("AsteriskVM sent updated message list")
|
|
|
|
self.messages = sorted(
|
|
|
|
msg, key=lambda item: item['info']['origtime'], reverse=True)
|
|
|
|
async_dispatcher_send(
|
|
|
|
self.hass, SIGNAL_MESSAGE_UPDATE, self.messages)
|
2017-08-06 18:19:47 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _request_messages(self):
|
|
|
|
"""Handle changes to the mailbox."""
|
2018-01-18 22:04:18 +00:00
|
|
|
_LOGGER.debug("Requesting message list")
|
2017-08-06 18:19:47 +00:00
|
|
|
self.client.messages()
|