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

Adding Sefton Council #2931

Closed

Conversation

northerngeek
Copy link
Contributor

First bit of Python (and I'm sure it shows!) so feel free to provide any critiques and if anybody wants to suggest how to make the GUI prompts work better I'm all ears. That said, it does manage to retrieve information from Sefton Council's webpages for bin collections, so it's a (functioning) start!

@1x02d8
Copy link

1x02d8 commented Oct 27, 2024

Very much appreciated @northerngeek! I've not been able to configure this add-on before - will give it a try right now. Hello from Waterloo, Liverpool!

@1x02d8
Copy link

1x02d8 commented Oct 27, 2024

@northerngeek what would the yaml waste_collection_schedule look like to access your data source please? I can see that this is what I need to supply to get started with this HACS add-on

waste_collection_schedule:
sources:
- name: SOURCE
args:
arg1: ARG1
arg2: ARG2
arg3: ARG3
customize:
- type: TYPE
alias: ALIAS
show: SHOW
icon: ICON
picture: PICTURE
use_dedicated_calendar: USE_DEDICATED_CALENDAR
dedicated_calendar_title: DEDICATED_CALENDAR_TITLE
day_offset: DAY_OFFSET
calendar_title: CALENDAR_TITLE
fetch_time: FETCH_TIME
random_fetch_time_offset: RANDOM_FETCH_TIME_OFFSET
day_switch_time: DAY_SWITCH_TIME
separator: SEPARATOR

Same for the sensor yaml please - apologies I'm a total newb in yaml and configuration HACS add-ons

sensor:

  • platform: waste_collection_schedule
    source_index: SOURCE_INDEX # (YAML only)
    name: NAME
    details_format: DETAILS_FORMAT
    count: COUNT
    leadtime: LEADTIME
    value_template: VALUE_TEMPLATE
    date_template: DATE_TEMPLATE
    add_days_to: ADD_DAYS_TO
    event_index: EVENT_INDEX
    types:
    • Waste Type 1
    • Waste Type 2

Thanks
John

@northerngeek
Copy link
Contributor Author

northerngeek commented Oct 28, 2024

Hi John/@1x02d8 ,

Greetings from Formby! Hopefully it gets approved soon to be merged (or some kind person helps me with making it ready to be merged) and you can just configure everything via the UI (much easier than YAML!) in the standard way.

I didn't personally use YAML so couldn't provide a tonne of insight sorry - in order to test this was working for me, I "simply"*:

  • Installed Waste Collection Schedule through HACS
  • Added the files that were changed in my pull request (accessing my Home Assistant via network file share) - I think sources.json and sefton_gov_uk.py are the main ones but you probably would be best to add/update all of them
  • Come to think of it, until the changes are merged you might be able to add my repo as a repo in HACS and install directly with my changes.... then install the real deal when/if they accept my pull request so you continue to get updates. Adding a custom repository in HACS is fairly straightforward (click the ... menu and go to Custom Repositories, add the URL of my repo there.
  • Then use the GUI by adding the Waste Collection Schedule as an integration as you normally would do in Home Assistant. The prompts it provides guide you but the help text on this repository make everything make a LOT more sense.

* I say simply but none of this is ever simple!

Good luck!
John

@1x02d8
Copy link

1x02d8 commented Oct 28, 2024

You're a star John - thanks so much. I already have the repo installed so I'll wait for the merge to be approved and I'll re-download the repository and configure via the front-end. Fingers crossed, your hard work will benefit others. Did you manage access an ICS file or did you have to extract the dates info from the PDF?

@northerngeek
Copy link
Contributor Author

No iCAL I'm afraid, and PDF is a nightmare format as far as I'm concerned - I just used Requests and BeautifulSoup (Python libraries) to navigate through the forms on the Sefton website, and extract the next dates from the webpage that loads - one downside of this is you only get the next collection, rather than a full year's worth. Looking at the PDF again, I suppose I could try and download it, find text that's coloured white and try to use that, although the "Arrangements to be confirmed" areas would be tricky. Differentiating between brown and grey coloured tables may be tricky too - I've tinkered with PDFs in C# before and the way the page is assembled is a bit like stamping layers of elements on top of each other (or it felt that way at least), so what would be trivial in a Word Processor (e.g. "how big are this document's margins") is surprisingly complex.

... If further out dates would be helpful, I'll try and figure out if there's a way.


class Source:
def __init__(self, houseNumberOrName:str | int, Streetname:str, Postcode:str): # argX correspond to the args dict in the source configuration
self._houseNumberOrName = houseNumberOrName.upper()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self._houseNumberOrName = houseNumberOrName.upper() should probably be self._houseNumberOrName = str(houseNumberOrName).upper() as this fails if houseNumberOrName is an integer (only happens using YAML configuration in the real world) you could modify one of your test case to have an integer houseNumberOrName

Comment on lines 44 to 47
def __init__(self, houseNumberOrName:str | int, Streetname:str, Postcode:str): # argX correspond to the args dict in the source configuration
self._houseNumberOrName = houseNumberOrName.upper()
self._Streetname = Streetname
self._Postcode = Postcode
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer python naming conventions (variables not capitalized and using Snake case): houseNumberOrName -> house_number_or_name and Streetname -> streetname and Postcode -> postcode

don't forget to change PARAM_DESCRIPTIONS as well

I know there are some other sources not using this convention but it's probably best to use them

Comment on lines 69 to 70
break
request = sess.post('https://www.sefton.gov.uk/bins-and-recycling/bins-and-recycling/when-is-my-bin-collection-day/', data=payload)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add

            if 'selectedValue' not in payload:
                raise SourceArgumentNotFoundWithSuggestions("houseNumberOrName", self._houseNumberOrName,[option.text.strip() for option in option_tags])

below the for so you'll get an error message suggesting alternatives if you enter something invalid

(you may need to rename variable as described above)

args:
Postcode: Postcode
Streetname: Streetname
House Number Or Name: houseNumberOrName
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Key must be written exactly like the constructor variable name (houseNumberOrName instead of House Number Or Name)

**Streetname**
*(string) (required)*

**House Name or Number**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be the name of the YAML key

@northerngeek
Copy link
Contributor Author

Replaced by #2947

@1x02d8
Copy link

1x02d8 commented Oct 31, 2024

Great job John! That's configured via the front-end and working fine for me. HA is now correctly telling me to put my black bin out this Friday and my brown bin next Friday - love it!!! Once they start collecting the green bins - will this collection date appear also?

Very impressed by your Python skills to dig this info from a nebulous web page

@northerngeek
Copy link
Contributor Author

Hi @1x02d8,

Fantastic, I'm really pleased it's working for you. There's still one more green bin collection for my road this year and it shows up, looking at a random address in Waterloo I can see the way things are formatted is very slightly different but I think the code will still work. If the green bins don't appear when they resume, feel free to send me a message and I'll happily tweak it.

You're too kind with your comments on my Python, but I'm really pleased it was helpful to somebody else, that's made my day!

@1x02d8
Copy link

1x02d8 commented Oct 31, 2024

Two happy Northern Geeks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants