Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract configuration into separate file, added LANG parameter #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions configfile.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[openweather]
api_key = YOUR_OPENWEATHER_APP_ID
lang = en
city_id = YOUR_OPENWEATHER_CITY_ID

[service]
host = mosquitto.local
port = 1883
topic = openweather

[client]
client_id = openweather-mqtt-service
26 changes: 18 additions & 8 deletions openweather_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@

import paho.mqtt.publish as publish
import requests
import configparser

# Config from environment (see Dockerfile)
OPENWEATHER_APP_ID = os.getenv('OPENWEATHER_APP_ID', 'YOUR_OPENWEATHER_APP_ID')
OPENWEATHER_CITY_ID = os.getenv('OPENWEATHER_CITY_ID', 'YOUR_OPENWEATHER_CITY_ID')
#Read config.ini file
config_obj = configparser.ConfigParser()
config_obj.read("configfile.ini")

MQTT_SERVICE_HOST = os.getenv('MQTT_SERVICE_HOST', 'mosquitto.local')
MQTT_SERVICE_PORT = int(os.getenv('MQTT_SERVICE_PORT', 1883))
MQTT_SERVICE_TOPIC = os.getenv('MQTT_SERVICE_TOPIC', 'openweather')
MQTT_CLIENT_ID = os.getenv('HOSTNAME', 'openweather-mqtt-service')
openweather_param = config_obj["openweather"]
service_param = config_obj["service"]
client_param = config_obj["client"]

OPENWEATHER_APP_ID = openweather_param["api_key"]
OPENWEATHER_APP_LANG = openweather_param["lang"]
OPENWEATHER_CITY_ID = openweather_param["city_id"]

MQTT_SERVICE_HOST = service_param["host"]
MQTT_SERVICE_PORT = int(service_param["port"])
MQTT_SERVICE_TOPIC = service_param["topic"]

MQTT_CLIENT_ID = client_param["client_id"]

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(name)s] %(levelname)8s %(message)s')
logger = logging.getLogger(MQTT_CLIENT_ID)
Expand Down Expand Up @@ -61,7 +71,7 @@ def unpack(parent_key, parent_value):
try:

logger.info("Connecting to OpenWeather for fresh weather information.")
url = f"http://api.openweathermap.org/data/2.5/weather?id={OPENWEATHER_CITY_ID}&appid={OPENWEATHER_APP_ID}&type=accurate&units=metric&lang=fr"
url = f"http://api.openweathermap.org/data/2.5/weather?id={OPENWEATHER_CITY_ID}&appid={OPENWEATHER_APP_ID}&type=accurate&units=metric&lang={OPENWEATHER_APP_LANG}"
r = requests.get(url)
data = r.json()

Expand Down