Use more narrow exception catching in nest (#63225)

This commit is contained in:
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 aiohttp import web
from google_nest_sdm.event import EventMessage from google_nest_sdm.event import EventMessage
from google_nest_sdm.exceptions import ( from google_nest_sdm.exceptions import (
ApiException,
AuthException, AuthException,
ConfigurationException, ConfigurationException,
GoogleNestException, SubscriberException,
) )
import voluptuous as vol import voluptuous as vol
@ -242,7 +243,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.error("Configuration error: %s", err) _LOGGER.error("Configuration error: %s", err)
subscriber.stop_async() subscriber.stop_async()
return False return False
except GoogleNestException as err: except SubscriberException as err:
if DATA_NEST_UNAVAILABLE not in hass.data[DOMAIN]: if DATA_NEST_UNAVAILABLE not in hass.data[DOMAIN]:
_LOGGER.error("Subscriber error: %s", err) _LOGGER.error("Subscriber error: %s", err)
hass.data[DOMAIN][DATA_NEST_UNAVAILABLE] = True hass.data[DOMAIN][DATA_NEST_UNAVAILABLE] = True
@ -251,7 +252,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
try: try:
await subscriber.async_get_device_manager() await subscriber.async_get_device_manager()
except GoogleNestException as err: except ApiException as err:
if DATA_NEST_UNAVAILABLE not in hass.data[DOMAIN]: if DATA_NEST_UNAVAILABLE not in hass.data[DOMAIN]:
_LOGGER.error("Device manager error: %s", err) _LOGGER.error("Device manager error: %s", err)
hass.data[DOMAIN][DATA_NEST_UNAVAILABLE] = True 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) _LOGGER.debug("Deleting subscriber '%s'", subscriber.subscriber_id)
try: try:
await subscriber.delete_subscription() await subscriber.delete_subscription()
except GoogleNestException as err: except (AuthException, SubscriberException) as err:
_LOGGER.warning( _LOGGER.warning(
"Unable to delete subscription '%s'; Will be automatically cleaned up by cloud console: %s", "Unable to delete subscription '%s'; Will be automatically cleaned up by cloud console: %s",
subscriber.subscriber_id, subscriber.subscriber_id,
@ -334,7 +335,7 @@ class NestEventMediaView(HomeAssistantView):
) )
try: try:
event_media = await nest_device.event_media_manager.get_media(event_id) 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 raise HomeAssistantError("Unable to fetch media for event") from err
if not event_media: if not event_media:
return self._json_error( 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.device import Device
from google_nest_sdm.event import ImageEventBase 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 haffmpeg.tools import IMAGE_JPEG
from homeassistant.components.camera import SUPPORT_STREAM, Camera from homeassistant.components.camera import SUPPORT_STREAM, Camera
@ -50,7 +50,7 @@ async def async_setup_sdm_entry(
subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER] subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER]
try: try:
device_manager = await subscriber.async_get_device_manager() device_manager = await subscriber.async_get_device_manager()
except GoogleNestException as err: except ApiException as err:
raise PlatformNotReady from err raise PlatformNotReady from err
# Fetch initial data so we have data when entities subscribe. # Fetch initial data so we have data when entities subscribe.
@ -144,7 +144,7 @@ class NestCamera(Camera):
_LOGGER.debug("Fetching stream url") _LOGGER.debug("Fetching stream url")
try: try:
self._stream = await trait.generate_rtsp_stream() self._stream = await trait.generate_rtsp_stream()
except GoogleNestException as err: except ApiException as err:
raise HomeAssistantError(f"Nest API error: {err}") from err raise HomeAssistantError(f"Nest API error: {err}") from err
self._schedule_stream_refresh() self._schedule_stream_refresh()
assert self._stream assert self._stream
@ -174,7 +174,7 @@ class NestCamera(Camera):
_LOGGER.debug("Extending stream url") _LOGGER.debug("Extending stream url")
try: try:
self._stream = await self._stream.extend_rtsp_stream() self._stream = await self._stream.extend_rtsp_stream()
except GoogleNestException as err: except ApiException as err:
_LOGGER.debug("Failed to extend stream: %s", err) _LOGGER.debug("Failed to extend stream: %s", err)
# Next attempt to catch a url will get a new one # Next attempt to catch a url will get a new one
self._stream = None self._stream = None
@ -257,14 +257,14 @@ class NestCamera(Camera):
# pylint: disable=no-self-use # pylint: disable=no-self-use
try: try:
event_image = await trait.generate_active_event_image() 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) _LOGGER.debug("Unable to generate event image URL: %s", err)
return None return None
if not event_image: if not event_image:
return None return None
try: try:
return await event_image.contents() return await event_image.contents()
except GoogleNestException as err: except ApiException as err:
_LOGGER.debug("Unable to fetch event image: %s", err) _LOGGER.debug("Unable to fetch event image: %s", err)
return None return None
@ -289,6 +289,6 @@ class NestCamera(Camera):
trait: CameraLiveStreamTrait = self._device.traits[CameraLiveStreamTrait.NAME] trait: CameraLiveStreamTrait = self._device.traits[CameraLiveStreamTrait.NAME]
try: try:
stream = await trait.generate_web_rtc_stream(offer_sdp) 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 raise HomeAssistantError(f"Nest API error: {err}") from err
return stream.answer_sdp 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 import Device
from google_nest_sdm.device_traits import FanTrait, TemperatureTrait 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 ( from google_nest_sdm.thermostat_traits import (
ThermostatEcoTrait, ThermostatEcoTrait,
ThermostatHeatCoolTrait, ThermostatHeatCoolTrait,
@ -90,7 +90,7 @@ async def async_setup_sdm_entry(
subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER] subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER]
try: try:
device_manager = await subscriber.async_get_device_manager() device_manager = await subscriber.async_get_device_manager()
except GoogleNestException as err: except ApiException as err:
raise PlatformNotReady from err raise PlatformNotReady from err
entities = [] entities = []

View file

@ -35,7 +35,7 @@ import async_timeout
from google_nest_sdm.exceptions import ( from google_nest_sdm.exceptions import (
AuthException, AuthException,
ConfigurationException, ConfigurationException,
GoogleNestException, SubscriberException,
) )
import voluptuous as vol import voluptuous as vol
@ -285,7 +285,7 @@ class NestFlowHandler(
except ConfigurationException as err: except ConfigurationException as err:
_LOGGER.error("Configuration error creating subscription: %s", err) _LOGGER.error("Configuration error creating subscription: %s", err)
errors[CONF_CLOUD_PROJECT_ID] = "bad_project_id" errors[CONF_CLOUD_PROJECT_ID] = "bad_project_id"
except GoogleNestException as err: except SubscriberException as err:
_LOGGER.error("Error creating subscription: %s", err) _LOGGER.error("Error creating subscription: %s", err)
errors[CONF_CLOUD_PROJECT_ID] = "subscriber_error" 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 import Device
from google_nest_sdm.device_traits import HumidityTrait, TemperatureTrait 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 ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
@ -40,7 +40,7 @@ async def async_setup_sdm_entry(
subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER] subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER]
try: try:
device_manager = await subscriber.async_get_device_manager() device_manager = await subscriber.async_get_device_manager()
except GoogleNestException as err: except ApiException as err:
_LOGGER.warning("Failed to get devices: %s", err) _LOGGER.warning("Failed to get devices: %s", err)
raise PlatformNotReady from err raise PlatformNotReady from err

View file

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

View file

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