Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions homeassistant/components/met/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
type MetWeatherConfigEntry = ConfigEntry[MetDataUpdateCoordinator]


class CannotConnect(HomeAssistantError):
"""Unable to connect to the web site."""


class MetWeatherData:
"""Keep data for Met.no weather entities."""

Expand Down Expand Up @@ -79,7 +75,10 @@ async def fetch_data(self) -> Self:
"""Fetch data from API - (current weather and forecast)."""
resp = await self._weather_data.fetching_data()
if not resp:
raise CannotConnect
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="cannot_connect",
)
self.current_weather_data = self._weather_data.get_current_weather()
time_zone = dt_util.get_default_time_zone()
self.daily_forecast = self._weather_data.get_forecast(time_zone, False, 0)
Expand Down Expand Up @@ -117,7 +116,11 @@ async def _async_update_data(self) -> MetWeatherData:
try:
return await self.weather.fetch_data()
except Exception as err:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so 2 things

  1. Ideally we don't catch raw Exceptions outside of the config flow and outside of tasks, so IMO we should just remove this except. This might uncover some bugs like a ValueError or AttributeError, but we should watch for those and properly fix the code
  2. You're now raising a translated exception to be caught and to be raised again as translated exception, I think we can just do this once

raise UpdateFailed(f"Update failed: {err}") from err
raise UpdateFailed(
translation_domain=DOMAIN,
translation_key="update_failed",
translation_placeholders={"error": str(err)},
) from err

def track_home(self) -> None:
"""Start tracking changes to HA home setting."""
Expand Down
8 changes: 8 additions & 0 deletions homeassistant/components/met/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
}
}
},
"exceptions": {
"cannot_connect": {
"message": "Unable to connect to the web site."
},
"update_failed": {
"message": "Update of data from the web site failed: {error}"
}
},
"options": {
"step": {
"init": {
Expand Down
Loading