mirror of
https://github.com/home-assistant/core
synced 2024-09-06 10:29:55 +02:00
iOS component hotfixes (#4015)
* iOS component hot fixes around component/platform loading, logging, and more * Load device_tracker and zeroconf in deps instead of bootstraping * Change conditional check on status code
This commit is contained in:
parent
5df8477536
commit
626763a7c3
@ -13,8 +13,6 @@ from voluptuous.humanize import humanize_error
|
||||
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
import homeassistant.loader as loader
|
||||
|
||||
from homeassistant.helpers import discovery
|
||||
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
@ -22,13 +20,11 @@ from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.const import (HTTP_INTERNAL_SERVER_ERROR,
|
||||
HTTP_BAD_REQUEST)
|
||||
|
||||
from homeassistant.components.notify import DOMAIN as NotifyDomain
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = "ios"
|
||||
|
||||
DEPENDENCIES = ["http"]
|
||||
DEPENDENCIES = ["device_tracker", "http", "zeroconf"]
|
||||
|
||||
CONF_PUSH = "push"
|
||||
CONF_PUSH_CATEGORIES = "categories"
|
||||
@ -245,34 +241,17 @@ def setup(hass, config):
|
||||
if CONFIG_FILE == {}:
|
||||
CONFIG_FILE[ATTR_DEVICES] = {}
|
||||
|
||||
device_tracker = loader.get_component("device_tracker")
|
||||
if device_tracker.DOMAIN not in hass.config.components:
|
||||
device_tracker.setup(hass, {})
|
||||
# Need this to enable requirements checking in the app.
|
||||
hass.config.components.append(device_tracker.DOMAIN)
|
||||
|
||||
if "notify.ios" not in hass.config.components:
|
||||
notify = loader.get_component("notify.ios")
|
||||
notify.get_service(hass, {})
|
||||
# Need this to enable requirements checking in the app.
|
||||
if NotifyDomain not in hass.config.components:
|
||||
hass.config.components.append(NotifyDomain)
|
||||
|
||||
zeroconf = loader.get_component("zeroconf")
|
||||
if zeroconf.DOMAIN not in hass.config.components:
|
||||
zeroconf.setup(hass, config)
|
||||
# Need this to enable requirements checking in the app.
|
||||
hass.config.components.append(zeroconf.DOMAIN)
|
||||
# Notify needs to have discovery
|
||||
# notify_config = {"notify": {CONF_PLATFORM: "ios"}}
|
||||
# bootstrap.setup_component(hass, "notify", notify_config)
|
||||
|
||||
discovery.load_platform(hass, "sensor", DOMAIN, {}, config)
|
||||
|
||||
hass.wsgi.register_view(iOSIdentifyDeviceView(hass))
|
||||
|
||||
if config.get(DOMAIN) is not None:
|
||||
app_config = config[DOMAIN]
|
||||
if app_config.get(CONF_PUSH) is not None:
|
||||
push_config = app_config[CONF_PUSH]
|
||||
hass.wsgi.register_view(iOSPushConfigView(hass, push_config))
|
||||
app_config = config.get(DOMAIN, {})
|
||||
hass.wsgi.register_view(iOSPushConfigView(hass,
|
||||
app_config.get(CONF_PUSH, {})))
|
||||
|
||||
return True
|
||||
|
||||
|
@ -23,6 +23,22 @@ PUSH_URL = "https://ios-push.home-assistant.io/push"
|
||||
DEPENDENCIES = ["ios"]
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def log_rate_limits(target, resp, level=20):
|
||||
"""Output rate limit log line at given level."""
|
||||
rate_limits = resp["rateLimits"]
|
||||
resetsAt = dt_util.parse_datetime(rate_limits["resetsAt"])
|
||||
resetsAtTime = resetsAt - datetime.now(timezone.utc)
|
||||
rate_limit_msg = ("iOS push notification rate limits for %s: "
|
||||
"%d sent, %d allowed, %d errors, "
|
||||
"resets in %s")
|
||||
_LOGGER.log(level, rate_limit_msg,
|
||||
ios.device_name_for_push_id(target),
|
||||
rate_limits["successful"],
|
||||
rate_limits["maximum"], rate_limits["errors"],
|
||||
str(resetsAtTime).split(".")[0])
|
||||
|
||||
|
||||
def get_service(hass, config):
|
||||
"""Get the iOS notification service."""
|
||||
if "notify.ios" not in hass.config.components:
|
||||
@ -66,22 +82,17 @@ class iOSNotificationService(BaseNotificationService):
|
||||
|
||||
req = requests.post(PUSH_URL, json=data, timeout=10)
|
||||
|
||||
if req.status_code is not 201:
|
||||
message = req.json()["message"]
|
||||
if req.status_code is 429:
|
||||
if req.status_code != 201:
|
||||
fallback_error = req.json().get("errorMessage",
|
||||
"Unknown error")
|
||||
fallback_message = ("Internal server error, "
|
||||
"please try again later: "
|
||||
"{}").format(fallback_error)
|
||||
message = req.json().get("message", fallback_message)
|
||||
if req.status_code == 429:
|
||||
_LOGGER.warning(message)
|
||||
elif req.status_code is 400 or 500:
|
||||
log_rate_limits(target, req.json(), 30)
|
||||
else:
|
||||
_LOGGER.error(message)
|
||||
|
||||
if req.status_code in (201, 429):
|
||||
rate_limits = req.json()["rateLimits"]
|
||||
resetsAt = dt_util.parse_datetime(rate_limits["resetsAt"])
|
||||
resetsAtTime = resetsAt - datetime.now(timezone.utc)
|
||||
rate_limit_msg = ("iOS push notification rate limits for %s: "
|
||||
"%d sent, %d allowed, %d errors, "
|
||||
"resets in %s")
|
||||
_LOGGER.info(rate_limit_msg,
|
||||
ios.device_name_for_push_id(target),
|
||||
rate_limits["successful"],
|
||||
rate_limits["maximum"], rate_limits["errors"],
|
||||
str(resetsAtTime).split(".")[0])
|
||||
else:
|
||||
log_rate_limits(target, req.json())
|
||||
|
Loading…
Reference in New Issue
Block a user