Skip to content

feat(midsussex_gov_uk): Add replacement source script and documentation#5083

Open
Gdkkauowj33 wants to merge 1 commit intomampfes:masterfrom
Gdkkauowj33:fixing_midsussex_gov_uk
Open

feat(midsussex_gov_uk): Add replacement source script and documentation#5083
Gdkkauowj33 wants to merge 1 commit intomampfes:masterfrom
Gdkkauowj33:fixing_midsussex_gov_uk

Conversation

@Gdkkauowj33
Copy link

Test Output

Testing source midsussex_gov_uk ...
found 10 entries for Test Case 1: 23 High Street
found 10 entries for Test Case 2: Hapstead Hall
found 10 entries for Test Case 3: The Gardeners Arms

@Gdkkauowj33
Copy link
Author

Mid Sussex changed to a new service provider, which presented a completely new web site and sequencing, hence fix required.

@KarlocWow
Copy link

Appears to work fine, thanks

@KaliPete
Copy link

For the less capable amongst us, what do we need to do to get Mid Sussex working again?
(I tried replacing the code in /homeassistant/custom_components/waste_collection_schedule/source/midsussex_gov_uk.py with the code I found on 7b9bf2d and restarted HA, but it still errored out the same way)

@Gdkkauowj33
Copy link
Author

You can either wait until the changes are merged and then update as you would normally. Or you can copy the file from the link below. It’s possible that you might need to update your address settings in the GUI. https://github.com/mampfes/hacs_waste_collection_schedule/blob/7b9bf2d72a5594706ee0cf77d55cc906be193516/custom_components/waste_collection_schedule/waste_collection_schedule/source/midsussex_gov_uk.py

@KarlocWow
Copy link

I think I had to delete the existing entity then add the integration as if new (after copying the updated py file and restarting HA) either because the source fields changed or because I was incompetent.

Copy link
Collaborator

@5ila5 5ila5 left a comment

Choose a reason for hiding this comment

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

You should either keep the __init__ variables or write a migration. So at least for configuration that used house_number street and postcode the source works again without reconfiguration.

a migritation would look like that:

diff --git a/custom_components/waste_collection_schedule/const.py b/custom_components/waste_collection_schedule/const.py
index 48e428da..f728d089 100644
--- a/custom_components/waste_collection_schedule/const.py
+++ b/custom_components/waste_collection_schedule/const.py
@@ -8,7 +8,7 @@ DOMAIN: Final = "waste_collection_schedule"
 UPDATE_SENSORS_SIGNAL: Final = "wcs_update_sensors_signal"
 
 CONFIG_VERSION: Final = 2
-CONFIG_MINOR_VERSION: Final = 10
+CONFIG_MINOR_VERSION: Final = 11
 
 # Config var names
 CONF_SOURCES: Final = "sources"
diff --git a/custom_components/waste_collection_schedule/init_ui.py b/custom_components/waste_collection_schedule/init_ui.py
index 2bc448cf..44d711ae 100644
--- a/custom_components/waste_collection_schedule/init_ui.py
+++ b/custom_components/waste_collection_schedule/init_ui.py
@@ -284,6 +284,23 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
                 if "name" in new_data["args"]:
                     del new_data["args"]["name"]
 
+        if config_entry.version < 2 or (
+            config_entry.version == 2 and config_entry.minor_version < 11
+        ):
+            # Migrate from birmingham_gov_uk to roundlookup_uk
+            if new_data.get("name", "") == "midsussex_gov_uk":
+                _LOGGER.debug("Migrating midsussex_gov_uk to new configuration")
+
+                new_data["args"]["number"] = new_data["args"].get("house_number")
+                new_data["args"]["post_code"] = new_data["args"].get("uprn")
+
+                if "house_number" in new_data["args"]:
+                    del new_data["args"]["house_number"]
+                if "house_name" in new_data["args"]:
+                    del new_data["args"]["house_name"]
+                if "address" in new_data["args"]:
+                    del new_data["args"]["address"]
+
         hass.config_entries.async_update_entry(
             config_entry,
             data=new_data,

Comment on lines +117 to 120
raise SourceArgumentException(
"Address not found. Check house name/number and postcode.",
argument="number"
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please look at the classes before you use them

class SourceArgumentException(Exception):
  def __init__(self, argument: str, message: str) -> None:

so this code generates a SourceArgumentException.__init__() got multiple values for argument 'argument'

I'd suggest raising a SourceArgumentNotFound here

Suggested change
raise SourceArgumentException(
"Address not found. Check house name/number and postcode.",
argument="number"
)
raise SourceArgumentNotFound(
"number",
self._number
)


if not collection_entries:
if soup3.find(string=lambda t: t and "not available" in t.lower()):
return [] # Return empty list if schedule is unavailable
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do not return an empty list as this will just fail silently. Raise an exception here so something shows up in the logs

try:
collection_date = datetime.datetime.strptime(date_str, DATE_FORMAT).date()
except ValueError:
print(f"Skipping entry with unparsable date format: {date_str}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you want this to show up in the HA logs, you need to use the logging library

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.

4 participants