Skip to content

Instantly share code, notes, and snippets.

@soundstorm
Last active January 11, 2023 18:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soundstorm/802986cebd52d9b4a3985879e1f514c1 to your computer and use it in GitHub Desktop.
Save soundstorm/802986cebd52d9b4a3985879e1f514c1 to your computer and use it in GitHub Desktop.
AHA Hannover MQTT Abholtermine (Abfallwirtschaft Hannover)
#!/usr/bin/python
import re, requests
import paho.mqtt.client as mqtt
import time, datetime
data = {
'gemeinde': 'Hannover',
'strasse': '00590@Burgweg@', # Wert des Dropdowns
'hausnr': '5',
}
base_topic = "paul-dohrmann-schule/abfall/"
hass_topic = "homeassistant" # if set to None Home Assistant Auto Discovery is disabled
client = mqtt.Client("aha-abfall")
client.username_pw_set("user", "password")
client.connect("host", port=1883, keepalive=60)
morgen = (datetime.date.today() + datetime.timedelta(days=1)).strftime('%d.%m.%Y')
abfallarten = ['Restabfall', 'Bioabfall', 'Papier', 'Leichtverpackungen']
req = requests.post("https://www.aha-region.de/abholtermine/abfuhrkalender/", data=data)
for abfallart in abfallarten:
try:
beg = req.text.index('<td valign="top" class="mobile_max">%s' % (abfallart,))
end = req.text.index('</tr>', beg)
termine = re.findall("\w{2}, (\d{2}\.\d{2}\.\d{4})", req.text[beg:end])
client.publish("%s/%s", (basetopic, abfallart), termine[0], retain=True)
client.publish("%s/%s/morgen" % (basetopic, abfallart), '1' if (morgen == termine[0]) else '0' , retain=True)
if hass_topic != None:
client.publish("%s/binary_sensor/%s/config" % (hass_topic, abfallart), '{"stat_t":"%s/%s/morgen", "name": "%s", "pl_on": 1, "pl_off": 0}' % (base_topic, abfallart, abfallart), retain=True)
except(ValueError):
print("Abfallart %s nicht gefunden" % abfallart)
client.wait_for_publish()
client.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment