0Coletas de lixo no Home Assistant

I’ve been devel­op­ing my Home Assist­ant based smarthome setup for around a year now (since leav­ing SmartTh­ings) and after an ini­tial steep learn­ing curve I’ve been veyr happy with home assist­ant so I have star­ted work­ing to expand the things it does as I am con­if­dent it offers a long term basis for my smarthome. One thing I wanted to add was bin col­lec­tion data to remind me which bin is due when. As I live in Shef­field I needed to pull this data from the loc­al author­it­ies out-sources pro­vider (Veolia). The fol­low­ing details how I did this, build­ing very much on the work of oth­ers who have done it already for oth­er regions

I used a script which i mod­i­fied from vari­ous examples on a HA sup­port thread by Rob­Brad. For this script to run I needed to install the beau­ti­ful­soup mod­ule on home-assistant.

Instale o módulo python BeautifulSoup

  • Install the Super­visor add-on ‘SSH and Web Terminal’
  • Open the ter­min­al from the main left-hand menu
  • Use the com­mand pip3 install beautifulsoup4

Adicione um script

  • I use the ‘Samba Share’ super­visor addon to access my home assist­ant files dir­ectly from my win­dows PC — unless you already have a pre­ferred access meth­od I recom­mend doing the same
  • navegar para \\ha-ip-address config
  • cre­ate a scripts-python dobrador
  • Cre­ate a new python script in this folder — I called mine bin_collection.py
  • Insert the fol­low­ing script
    from bs4 import BeautifulSoup
    import datetime
    from dateutil import parser
    import requests
    import json
    
    url = 'https://wasteservices.sheffield.gov.uk/property/############'
    page = requests.get(url)
    
    if page.status_code != 200:
        Saída(1)
    
    sopa = BeautifulSoup(page.text, 'html.parser')
    
    out = {}
    bh3s = soup.find_all('td', class_ ="Nome do Serviço")
    bpds = soup.find_all('td', class_ ="próximo serviço")
    
    para eu no alcance(unicamente(bpds)):
        bin_colour = str(bh3s[eu].conteúdo[3]).mais baixo().Dividido('>')[1].Dividido('')[0]
        Fora[bin_colour] = parser.parse(bpds[eu].conteúdo[2].tira().Dividido(',')[0]).strftime('% Y-% m-% d')
    
    imprimir(json.dumps(Fora))
    
  • Você precisará substituir o ############ with the unique num­ber for your prop­erty which I will explain how to get in the next step

Obtenha seu endereço exclusivo

  • Navegue até https://wasteservices.sheffield.gov.uk/property/
  • Insira seu código postal e selecione seu endereço na lista
  • Anote o novo url. Use it in the url line in the script above — the only part you will need to change is the 12 digit num­ber at the end

Chame seu script de configuration.yaml

  • Uma simples entrada em configuration.yaml é tudo o que é necessário para fazer o script funcionar
  • You may want to adjust the fre­quency of the run. Mine runs once per day.
    sensor:
     - plataforma: command_line
       name: "Coletas de lixo"
       comando: "python3 /config/python-scripts/bin_collection.py"
       scan_interval: 86400
       command_timeout: 60 #necessário, pois o site é lento para responder e frequentemente atingirá o tempo limite se for deixado no padrão
    

Crie entidades para cada tipo de bin usando configuration.yaml

  • Imme­di­ately under the above sec­tion (então ainda sob o sensor: sec-ção) adicione o seguinte
     - plataforma: template
       sensors:
         black_bin:
           device_class: timestamp
           value_template: '{{ (estados("sensor.bin_collections")|from_json())["Preto"] }}'
           unique_id: "black_bin"
         brown_bin:
           device_class: timestamp
           value_template: '{{ (estados("sensor.bin_collections")|from_json())["Castanho"] }}'
           unique_id: "brown_bin"
         blue_bin:
           device_class: timestamp
           value_template: '{{ (estados("sensor.bin_collections")|from_json())["azul"] }}'
           unique_id: "blue_bin"
    

Exibir os resultados no front end

  • To show the res­ults on the front end simply add a card with the 3 sensores nele (spe­cific­ally sensor.black_bin, sensor.blue_bin, e sensor.brown_bin)
  • Note you may need to restart home assist­ant to load the new sensors you added to configuration.yaml

Acho que perdemos algo? Deixe-nos saber por comentar abaixo. Se você deseja se inscrever, use o link de inscrição no menu no canto superior direito. Você também pode compartilhar isso com seus amigos usando os links sociais abaixo. Felicidades.

Deixe uma resposta