diff --git a/.coveragerc b/.coveragerc index 3cbb942508b..8297f7823d5 100644 --- a/.coveragerc +++ b/.coveragerc @@ -384,6 +384,7 @@ omit = homeassistant/components/notify/free_mobile.py homeassistant/components/notify/gntp.py homeassistant/components/notify/group.py + homeassistant/components/notify/hipchat.py homeassistant/components/notify/instapush.py homeassistant/components/notify/kodi.py homeassistant/components/notify/lannouncer.py diff --git a/homeassistant/components/notify/hipchat.py b/homeassistant/components/notify/hipchat.py new file mode 100644 index 00000000000..5d69ce97d61 --- /dev/null +++ b/homeassistant/components/notify/hipchat.py @@ -0,0 +1,102 @@ +""" +HipChat platform for notify component. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/notify.hipchat/ +""" +import logging + +import voluptuous as vol + +from homeassistant.components.notify import ( + ATTR_TARGET, ATTR_DATA, + PLATFORM_SCHEMA, BaseNotificationService) +from homeassistant.const import ( + CONF_TOKEN) +import homeassistant.helpers.config_validation as cv + +REQUIREMENTS = ['hipnotify==1.0.8'] + +_LOGGER = logging.getLogger(__name__) + +CONF_COLOR = 'color' +CONF_ROOM = 'room' +CONF_NOTIFY = 'notify' +CONF_FORMAT = 'format' +CONF_HOST = 'host' + +VALID_COLORS = {'yellow', 'green', 'red', 'purple', 'gray', 'random'} +VALID_FORMATS = {'text', 'html'} + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_TOKEN): cv.string, + vol.Required(CONF_ROOM): vol.Coerce(int), + vol.Optional(CONF_COLOR, default='yellow'): vol.In(VALID_COLORS), + vol.Optional(CONF_NOTIFY, default=False): cv.boolean, + vol.Optional(CONF_FORMAT, default='text'): vol.In(VALID_FORMATS), + vol.Optional(CONF_HOST, default='https://api.hipchat.com/'): cv.string, +}) + + +def get_service(hass, config, discovery_info=None): + """Get the HipChat notification service.""" + return HipchatNotificationService( + config[CONF_TOKEN], + config[CONF_ROOM], + config[CONF_COLOR], + config[CONF_NOTIFY], + config[CONF_FORMAT], + config[CONF_HOST]) + + +class HipchatNotificationService(BaseNotificationService): + """Implement the notification service for HipChat.""" + + def __init__(self, token, default_room, default_color, default_notify, + default_format, host): + """Initialize the service.""" + self._token = token + self._default_room = default_room + self._default_color = default_color + self._default_notify = default_notify + self._default_format = default_format + self._host = host + + self._rooms = {} + self._get_room(self._default_room) + + def _get_room(self, room): + """Get Room object, creating it if necessary.""" + from hipnotify import Room + if room not in self._rooms: + self._rooms[room] = Room(token=self._token, + room_id=room, + endpoint_url=self._host) + return self._rooms[room] + + def send_message(self, message="", **kwargs): + """Send a message.""" + color = self._default_color + notify = self._default_notify + message_format = self._default_format + + if kwargs.get(ATTR_DATA) is not None: + data = kwargs.get(ATTR_DATA) + if ((data.get(CONF_COLOR) is not None) + and (data.get(CONF_COLOR) in VALID_COLORS)): + color = data.get(CONF_COLOR) + if ((data.get(CONF_NOTIFY) is not None) + and isinstance(data.get(CONF_NOTIFY), bool)): + notify = data.get(CONF_NOTIFY) + if ((data.get(CONF_FORMAT) is not None) + and (data.get(CONF_FORMAT) in VALID_FORMATS)): + message_format = data.get(CONF_FORMAT) + + targets = kwargs.get(ATTR_TARGET, [self._default_room]) + + for target in targets: + room = self._get_room(target) + room.notify(msg=message, + color=color, + notify=notify, + message_format=message_format) diff --git a/requirements_all.txt b/requirements_all.txt index 0a14bc0d38f..34a1482508b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -283,6 +283,9 @@ heatmiserV3==0.9.1 # homeassistant.components.switch.hikvisioncam hikvision==0.4 +# homeassistant.components.notify.hipchat +hipnotify==1.0.8 + # homeassistant.components.binary_sensor.workday holidays==0.8.1