Skip to content

Commit

Permalink
add HomeAssistantError to service calls on ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
pantherale0 committed Jan 5, 2024
1 parent c77fcc9 commit d30e5e0
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions custom_components/fuel_prices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ async def handle_fuel_lookup(call: ServiceCall) -> ServiceResponse:
lat = call.data.get("location", {}).get("latitude", 0.0)
long = call.data.get("location", {}).get("longitude", 0.0)
fuel_type = call.data.get("type")
return {
"fuels": await fuel_prices.find_fuel_from_point(
(lat, long), radius, fuel_type
)
}
try:
return {
"fuels": await fuel_prices.find_fuel_from_point(
(lat, long), radius, fuel_type
)
}
except ValueError as err:
raise HomeAssistantError("Country not available for fuel data.") from err

async def handle_fuel_location_lookup(call: ServiceCall) -> ServiceResponse:
"""Handle a fuel location lookup call."""
Expand All @@ -83,9 +86,12 @@ async def handle_fuel_location_lookup(call: ServiceCall) -> ServiceResponse:
radius = radius / 1609
lat = call.data.get("location", {}).get("latitude", 0.0)
long = call.data.get("location", {}).get("longitude", 0.0)
locations = await fuel_prices.find_fuel_locations_from_point(
(lat, long), radius
)
try:
locations = await fuel_prices.find_fuel_locations_from_point(
(lat, long), radius
)
except ValueError as err:
raise HomeAssistantError("Country not available for fuel data.") from err
locations_built = []
for loc in locations:
await loc.dynamic_build_fuels()
Expand Down

0 comments on commit d30e5e0

Please sign in to comment.