Skip to content

Instantly share code, notes, and snippets.

@soundstorm
Last active January 6, 2026 18:00
Show Gist options
  • Select an option

  • Save soundstorm/802986cebd52d9b4a3985879e1f514c1 to your computer and use it in GitHub Desktop.

Select an option

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',
'hausnraddon': '',
'ladeort': ''
}
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 abfall in abfall_types:
try:
beg = req.text.index('<strong>' + abfall['type'])
end = req.text.index('colspan="3"', beg)
termine = re.findall("\w{2}, (\d{2}\.\d{2}\.\d{4})", req.text[beg:end])
client.publish("homeassistant/binary_sensor/%s/config" % (abfall['type'],), '{"stat_t":"%s/%s/morgen", "uniq_id": "%s%s%s", "dev": {"ids":["ahaahaaha"], "name":"Abfall", "mf": "aha", "mdl":"Abfall","sw":"Ich bin Müll"}, "json_attr_t":"%s/%s/att", "name": "%s", "pl_on": 1, "pl_off": 0}' % (base_topic, abfall['type'], data['strasse'], data['hausnr'], abfall['type'], base_topic, abfall['type'], abfall['type']), retain=True)
client.publish("%s/%s" % (base_topic, abfall['type']), termine[0], retain=True)
client.publish("%s/%s/att" % (base_topic, abfall['type']), '{"next":"%s"}' % (termine[0],), retain=True)
client.publish("%s/%s/morgen" % (base_topic, abfall['type']), '1' if (morgen == termine[0]) else '0' , retain=True)
except(ValueError):
print("Abfallart nicht gefunden")
client.wait_for_publish()
client.disconnect()
@DonKracho
Copy link

Vielen Dank, exakt wonach ich gesucht habe. Über die Feiertage sind die Termine bei uns unregelmäßig, sodass schon Termine verpasst wurden.

Ich musste das Text-Suchfenster nur noch etwas an das aktuellen body der Seite anpassen und es funktioniert prima.

		beg = req.text.index('<strong>%s</strong>' % (abfallart,))
		end = req.text.index('<td valign="top" class="p-1">', beg)

@soundstorm
Copy link
Author

Hab das gist mal angepasst, hatte das lokal nur an die neue Struktur angepasst aber hier nicht entsprechend kopiert.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment