Skip to content

Commit

Permalink
feat: Add endpoint to fetch forecast for a specific location and futu…
Browse files Browse the repository at this point in the history
…re hour

- Added route `/locations/<name>/forecast/<int:number_of_hours>h` to fetch forecast for a specific location and number of hours into the future.
- Implemented logic to get the current datetime and retrieve the location.
- Integrated the get_forecast_for_future_hour method from the Location class to provide the forecast data.
- Included error handling for location not found and other exceptions.
- Bumped version of the API server to 1.4.8.
  • Loading branch information
aschmere committed Jun 16, 2024
1 parent 610f04d commit 860fab7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions dwd_global_rad_api_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import main
import logging
import dwd_global_radiation as dgr
from datetime import datetime, timezone

app = Flask(__name__)

Expand All @@ -29,6 +30,24 @@ def format(self, record):
app.logger.propagate = False


@app.route('/locations/<name>/forecast/<int:number_of_hours>h', methods=['GET'])
def get_forecast_for_future_hour(name, number_of_hours):
try:
# Get the current datetime
datetime_input = datetime.now(timezone.utc)

# Get the location
location = objGlobalRadiation.get_location_by_name(name)
if not location:
return jsonify({'error': 'Location not found'}), 404

# Get the forecast for the specified future hour
forecast = location.get_forecast_for_future_hour(datetime_input, number_of_hours)
return jsonify(forecast)
except Exception as e:
app.logger.error(f"Error fetching forecast for future hour: {e}")
return jsonify({'error': str(e)}), 500

@app.route('/process', methods=['POST'])
def process():
try:
Expand Down
2 changes: 1 addition & 1 deletion dwd_global_rad_api_server/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://developers.home-assistant.io/docs/add-ons/configuration#add-on-config
name: DWD Global Rad Api Server
version: "1.4.7"
version: "1.4.8"
slug: dwd_global_rad_api_server
description: DWD Global Rad Api Server
arch:
Expand Down

0 comments on commit 860fab7

Please sign in to comment.