Use more narrow exception catching in `nest` (#63225)

pull/63018/head^2
Allen Porter 2022-01-02 09:54:56 -08:00 committed by GitHub
parent b9daa22891
commit 15baea4ba3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 26 additions and 24 deletions

View File

@ -8,9 +8,10 @@ import logging
from aiohttp import web
from google_nest_sdm.event import EventMessage
from google_nest_sdm.exceptions import (
ApiException,
AuthException,
ConfigurationException,
GoogleNestException,
SubscriberException,
)
import voluptuous as vol
@ -242,7 +243,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.error("Configuration error: %s", err)
subscriber.stop_async()
return False
except GoogleNestException as err:
except SubscriberException as err:
if DATA_NEST_UNAVAILABLE not in hass.data[DOMAIN]:
_LOGGER.error("Subscriber error: %s", err)
hass.data[DOMAIN][DATA_NEST_UNAVAILABLE] = True
@ -251,7 +252,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
try:
await subscriber.async_get_device_manager()
except GoogleNestException as err:
except ApiException as err:
if DATA_NEST_UNAVAILABLE not in hass.data[DOMAIN]:
_LOGGER.error("Device manager error: %s", err)
hass.data[DOMAIN][DATA_NEST_UNAVAILABLE] = True
@ -293,7 +294,7 @@ async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
_LOGGER.debug("Deleting subscriber '%s'", subscriber.subscriber_id)
try:
await subscriber.delete_subscription()
except GoogleNestException as err:
except (AuthException, SubscriberException) as err:
_LOGGER.warning(
"Unable to delete subscription '%s'; Will be automatically cleaned up by cloud console: %s",
subscriber.subscriber_id,
@ -334,7 +335,7 @@ class NestEventMediaView(HomeAssistantView):
)
try:
event_media = await nest_device.event_media_manager.get_media(event_id)
except GoogleNestException as err:
except ApiException as err:
raise HomeAssistantError("Unable to fetch media for event") from err
if not event_media:
return self._json_error(

View File

@ -17,7 +17,7 @@ from google_nest_sdm.camera_traits import (
)
from google_nest_sdm.device import Device
from google_nest_sdm.event import ImageEventBase
from google_nest_sdm.exceptions import GoogleNestException
from google_nest_sdm.exceptions import ApiException
from haffmpeg.tools import IMAGE_JPEG
from homeassistant.components.camera import SUPPORT_STREAM, Camera
@ -50,7 +50,7 @@ async def async_setup_sdm_entry(
subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER]
try:
device_manager = await subscriber.async_get_device_manager()
except GoogleNestException as err:
except ApiException as err:
raise PlatformNotReady from err
# Fetch initial data so we have data when entities subscribe.
@ -144,7 +144,7 @@ class NestCamera(Camera):
_LOGGER.debug("Fetching stream url")
try:
self._stream = await trait.generate_rtsp_stream()
except GoogleNestException as err:
except ApiException as err:
raise HomeAssistantError(f"Nest API error: {err}") from err
self._schedule_stream_refresh()
assert self._stream
@ -174,7 +174,7 @@ class NestCamera(Camera):
_LOGGER.debug("Extending stream url")
try:
self._stream = await self._stream.extend_rtsp_stream()
except GoogleNestException as err:
except ApiException as err:
_LOGGER.debug("Failed to extend stream: %s", err)
# Next attempt to catch a url will get a new one
self._stream = None
@ -257,14 +257,14 @@ class NestCamera(Camera):
# pylint: disable=no-self-use
try:
event_image = await trait.generate_active_event_image()
except GoogleNestException as err:
except ApiException as err:
_LOGGER.debug("Unable to generate event image URL: %s", err)
return None
if not event_image:
return None
try:
return await event_image.contents()
except GoogleNestException as err:
except ApiException as err:
_LOGGER.debug("Unable to fetch event image: %s", err)
return None
@ -289,6 +289,6 @@ class NestCamera(Camera):
trait: CameraLiveStreamTrait = self._device.traits[CameraLiveStreamTrait.NAME]
try:
stream = await trait.generate_web_rtc_stream(offer_sdp)
except GoogleNestException as err:
except ApiException as err:
raise HomeAssistantError(f"Nest API error: {err}") from err
return stream.answer_sdp

View File

@ -5,7 +5,7 @@ from typing import Any, cast
from google_nest_sdm.device import Device
from google_nest_sdm.device_traits import FanTrait, TemperatureTrait
from google_nest_sdm.exceptions import GoogleNestException
from google_nest_sdm.exceptions import ApiException
from google_nest_sdm.thermostat_traits import (
ThermostatEcoTrait,
ThermostatHeatCoolTrait,
@ -90,7 +90,7 @@ async def async_setup_sdm_entry(
subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER]
try:
device_manager = await subscriber.async_get_device_manager()
except GoogleNestException as err:
except ApiException as err:
raise PlatformNotReady from err
entities = []

View File

@ -35,7 +35,7 @@ import async_timeout
from google_nest_sdm.exceptions import (
AuthException,
ConfigurationException,
GoogleNestException,
SubscriberException,
)
import voluptuous as vol
@ -285,7 +285,7 @@ class NestFlowHandler(
except ConfigurationException as err:
_LOGGER.error("Configuration error creating subscription: %s", err)
errors[CONF_CLOUD_PROJECT_ID] = "bad_project_id"
except GoogleNestException as err:
except SubscriberException as err:
_LOGGER.error("Error creating subscription: %s", err)
errors[CONF_CLOUD_PROJECT_ID] = "subscriber_error"

View File

@ -5,7 +5,7 @@ import logging
from google_nest_sdm.device import Device
from google_nest_sdm.device_traits import HumidityTrait, TemperatureTrait
from google_nest_sdm.exceptions import GoogleNestException
from google_nest_sdm.exceptions import ApiException
from homeassistant.components.sensor import (
SensorDeviceClass,
@ -40,7 +40,7 @@ async def async_setup_sdm_entry(
subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER]
try:
device_manager = await subscriber.async_get_device_manager()
except GoogleNestException as err:
except ApiException as err:
_LOGGER.warning("Failed to get devices: %s", err)
raise PlatformNotReady from err

View File

@ -6,7 +6,7 @@ from unittest.mock import patch
from google_nest_sdm.exceptions import (
AuthException,
ConfigurationException,
GoogleNestException,
SubscriberException,
)
import pytest
@ -473,7 +473,7 @@ async def test_pubsub_subscription_failure(hass, oauth):
await oauth.async_pubsub_flow(result)
with patch(
"homeassistant.components.nest.api.GoogleNestSubscriber.create_subscription",
side_effect=GoogleNestException(),
side_effect=SubscriberException(),
):
result = await oauth.async_configure(
result, {"cloud_project_id": CLOUD_PROJECT_ID}

View File

@ -10,9 +10,10 @@ import logging
from unittest.mock import patch
from google_nest_sdm.exceptions import (
ApiException,
AuthException,
ConfigurationException,
GoogleNestException,
SubscriberException,
)
from homeassistant.components.nest import DOMAIN
@ -66,7 +67,7 @@ async def test_setup_susbcriber_failure(hass, caplog):
"""Test configuration error."""
with patch(
"homeassistant.components.nest.api.GoogleNestSubscriber.start_async",
side_effect=GoogleNestException(),
side_effect=SubscriberException(),
), caplog.at_level(logging.ERROR, logger="homeassistant.components.nest"):
result = await async_setup_sdm(hass)
assert result
@ -83,7 +84,7 @@ async def test_setup_device_manager_failure(hass, caplog):
"homeassistant.components.nest.api.GoogleNestSubscriber.start_async"
), patch(
"homeassistant.components.nest.api.GoogleNestSubscriber.async_get_device_manager",
side_effect=GoogleNestException(),
side_effect=ApiException(),
), caplog.at_level(
logging.ERROR, logger="homeassistant.components.nest"
):
@ -252,7 +253,7 @@ async def test_remove_entry_delete_subscriber_failure(hass, caplog):
with patch(
"homeassistant.components.nest.api.GoogleNestSubscriber.delete_subscription",
side_effect=GoogleNestException(),
side_effect=SubscriberException(),
):
assert await hass.config_entries.async_remove(entry.entry_id)