Skip to content

Commit

Permalink
added configureable weather station provider
Browse files Browse the repository at this point in the history
  • Loading branch information
HolgerHees committed Mar 24, 2024
1 parent 783783b commit ea4fc89
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
7 changes: 5 additions & 2 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,11 @@ default_variables: {
dependency: "{{weather_service_enabled}}" },
weather_mqtt_server: { default: "{{'mosquitto' if mosquitto_enabled else 'dummy'}}", # 'mosquitto', 'cloud_mosquitto' or 'dummy'
dependency: "{{weather_service_enabled}}" },
weather_mqtt_publish_topic: { default: "{{server_name}}",
dependency: "{{weather_service_enabled and weather_api_provider != 'listener'}}" }, # the main topic prefix where you publish weather data
weather_mqtt_publish_provider_topic: { default: "{{server_name}}", # the main topic prefix where you publish weather data
dependency: "{{weather_service_enabled and weather_api_provider != 'listener'}}" },

weather_mqtt_station_consumer_topic: { optional: yes, # the topic prefix of a local weather station. If not provided, values are comming from weather_api_provider
dependency: "{{weather_service_enabled and weather_mqtt_server != 'dummy'}}" },
weather_api_username: { dependency: "{{weather_service_enabled and weather_api_provider == 'meteogroup'}}" },
weather_api_password: { dependency: "{{weather_service_enabled and weather_api_provider == 'meteogroup'}}" },
# *** END WEATHER SERVICE ***
Expand Down
7 changes: 5 additions & 2 deletions roles/weather_service/templates/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

api_password = {% if weather_api_password is defined %}"{{weather_api_password}}"{% else %}False{% endif %}

publish_topic = {% if weather_mqtt_publish_topic is defined %}"{{weather_mqtt_publish_topic}}"{% else %}False{% endif %}
mosquitto_host = "{{weather_mqtt_server}}"

publish_provider_topic = {% if weather_mqtt_publish_provider_topic is defined %}"{{weather_mqtt_publish_provider_topic}}"{% else %}False{% endif %}


station_consumer_topic = {% if weather_mqtt_station_consumer_topic is defined %}"{{weather_mqtt_station_consumer_topic}}"{% else %}False{% endif %}

mosquitto_host = "{{weather_mqtt_server}}"

db_host = "mysql"
db_name = "shared"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class StationConsumer():
def __init__(self, config, mqtt, provider_consumer):
self.mqtt = mqtt

self.config = config

self.is_running = False

self.dump_path = "{}consumer_station.json".format(config.lib_path)
Expand All @@ -27,7 +29,7 @@ def start(self):
self._restore()
if not os.path.exists(self.dump_path):
self._dump()
self.mqtt.subscribe('+/weather/station/#', self.on_message)
self.mqtt.subscribe( '{}/weather/station/#'.format(self.config.station_consumer_topic), self.on_message)
self.is_running = True

def terminate(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ def triggerSummerizedItems(self, db, mqtt):
for field in summeryFields:
tmp[field][2] = tmp[field][2] / len(result)

mqtt.publish("{}/weather/provider/items/{}/{}".format(self.config.publish_topic,field,"min"), payload=str(tmp[field][0]).encode("utf-8"), qos=0, retain=False)
mqtt.publish("{}/weather/provider/items/{}/{}".format(self.config.publish_topic,field,"max"), payload=str(tmp[field][1]).encode("utf-8"), qos=0, retain=False)
mqtt.publish("{}/weather/provider/items/{}/{}".format(self.config.publish_topic,field,"avg"), payload=str(tmp[field][2]).encode("utf-8"), qos=0, retain=False)
mqtt.publish("{}/weather/provider/items/{}/{}".format(self.config.publish_provider_topic,field,"min"), payload=str(tmp[field][0]).encode("utf-8"), qos=0, retain=False)
mqtt.publish("{}/weather/provider/items/{}/{}".format(self.config.publish_provider_topic,field,"max"), payload=str(tmp[field][1]).encode("utf-8"), qos=0, retain=False)
mqtt.publish("{}/weather/provider/items/{}/{}".format(self.config.publish_provider_topic,field,"avg"), payload=str(tmp[field][2]).encode("utf-8"), qos=0, retain=False)

for offset in summeryOffsets:
data = db.getOffset(offset)
for field, value in data.items():
mqtt.publish("{}/weather/provider/items/{}/{}".format(self.config.publish_topic,field,offset), payload=str(value).encode("utf-8"), qos=0, retain=False)
mqtt.publish("{}/weather/provider/items/refreshed".format(self.config.publish_topic), payload="1", qos=0, retain=False)
mqtt.publish("{}/weather/provider/items/{}/{}".format(self.config.publish_provider_topic,field,offset), payload=str(value).encode("utf-8"), qos=0, retain=False)
mqtt.publish("{}/weather/provider/items/refreshed".format(self.config.publish_provider_topic), payload="1", qos=0, retain=False)

logging.info("Summery data published")
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Handler(Server):
self.astro_consumer = AstroConsumer(config, self)
self.provider_consumer = ProviderConsumer(config, self.mqtt, self.db, self)

if config.mosquitto_host != "dummy":
if config.station_consumer_topic:
self.station_consumer = StationConsumer(config, self.mqtt, self.provider_consumer)
else:
self.station_consumer = None
Expand Down

0 comments on commit ea4fc89

Please sign in to comment.