mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Use consts and string formatting (#7243)
This commit is contained in:
parent
209da6f338
commit
fa4a912a86
@ -13,7 +13,8 @@ import voluptuous as vol
|
|||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components.notify import (
|
from homeassistant.components.notify import (
|
||||||
ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService)
|
ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService)
|
||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import (
|
||||||
|
CONF_API_KEY, HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
_RESOURCE = 'https://api.instapush.im/v1/'
|
_RESOURCE = 'https://api.instapush.im/v1/'
|
||||||
@ -24,6 +25,9 @@ CONF_TRACKER = 'tracker'
|
|||||||
|
|
||||||
DEFAULT_TIMEOUT = 10
|
DEFAULT_TIMEOUT = 10
|
||||||
|
|
||||||
|
HTTP_HEADER_APPID = 'x-instapush-appid'
|
||||||
|
HTTP_HEADER_APPSECRET = 'x-instapush-appsecret'
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
vol.Required(CONF_API_KEY): cv.string,
|
||||||
vol.Required(CONF_APP_SECRET): cv.string,
|
vol.Required(CONF_APP_SECRET): cv.string,
|
||||||
@ -34,25 +38,25 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
|
|
||||||
def get_service(hass, config, discovery_info=None):
|
def get_service(hass, config, discovery_info=None):
|
||||||
"""Get the Instapush notification service."""
|
"""Get the Instapush notification service."""
|
||||||
headers = {'x-instapush-appid': config[CONF_API_KEY],
|
headers = {
|
||||||
'x-instapush-appsecret': config[CONF_APP_SECRET]}
|
HTTP_HEADER_APPID: config[CONF_API_KEY],
|
||||||
|
HTTP_HEADER_APPSECRET: config[CONF_APP_SECRET],
|
||||||
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
'{}{}'.format(_RESOURCE, 'events/list'), headers=headers,
|
'{}{}'.format(_RESOURCE, 'events/list'), headers=headers,
|
||||||
timeout=DEFAULT_TIMEOUT).json()
|
timeout=DEFAULT_TIMEOUT).json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
_LOGGER.error('Unexpected answer from Instapush API.')
|
_LOGGER.error('Unexpected answer from Instapush API')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if 'error' in response:
|
if 'error' in response:
|
||||||
_LOGGER.error(response['msg'])
|
_LOGGER.error(response['msg'])
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if len([app for app in response
|
if not [app for app in response if app['title'] == config[CONF_EVENT]]:
|
||||||
if app['title'] == config[CONF_EVENT]]) == 0:
|
_LOGGER.error("No app match your given value")
|
||||||
_LOGGER.error("No app match your given value. "
|
|
||||||
"Please create an app at https://instapush.im")
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return InstapushNotificationService(
|
return InstapushNotificationService(
|
||||||
@ -70,16 +74,17 @@ class InstapushNotificationService(BaseNotificationService):
|
|||||||
self._event = event
|
self._event = event
|
||||||
self._tracker = tracker
|
self._tracker = tracker
|
||||||
self._headers = {
|
self._headers = {
|
||||||
'x-instapush-appid': self._api_key,
|
HTTP_HEADER_APPID: self._api_key,
|
||||||
'x-instapush-appsecret': self._app_secret,
|
HTTP_HEADER_APPSECRET: self._app_secret,
|
||||||
'Content-Type': 'application/json'}
|
HTTP_HEADER_CONTENT_TYPE: CONTENT_TYPE_JSON,
|
||||||
|
}
|
||||||
|
|
||||||
def send_message(self, message="", **kwargs):
|
def send_message(self, message="", **kwargs):
|
||||||
"""Send a message to a user."""
|
"""Send a message to a user."""
|
||||||
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||||
data = {
|
data = {
|
||||||
'event': self._event,
|
'event': self._event,
|
||||||
'trackers': {self._tracker: title + ' : ' + message}
|
'trackers': {self._tracker: '{} : {}'.format(title, message)}
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user