Skip to content

Commit

Permalink
fix optionsflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram van Dartel committed Dec 20, 2024
1 parent 7f70ba7 commit 3ded955
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 72 deletions.
64 changes: 0 additions & 64 deletions custom_components/afvalwijzer/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
"""
Config flow for the Afvalwijzer component
"""
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
import logging

Expand Down Expand Up @@ -57,11 +53,6 @@ async def async_step_user(self, user_input=None):
description_placeholders={},
)

@staticmethod
@callback
def async_get_options_flow(config_entry):
return AfvalwijzerOptionsFlow()

def _validate_postal_code(self, postal_code):
"""Validate the postal code format."""
return (
Expand All @@ -74,58 +65,3 @@ def _validate_postal_code(self, postal_code):
def _validate_street_number(self, street_number):
"""Validate the street number."""
return street_number.isdigit()







class AfvalwijzerOptionsFlow(config_entries.OptionsFlow):
"""Handle options for Afvalwijzer."""

async def async_step_init(self, user_input=None):
"""Handle options configuration."""
_LOGGER.debug(f"Config entry: {self.config_entry}")
errors = {}

# Ensure self.config_entry is valid
if not self.config_entry:
_LOGGER.error("Config entry is invalid.")
return self.async_abort(reason="invalid_config_entry")

current_options = self.config_entry.options

if user_input is not None:
# Merge the user input with the current options
updated_options = {**current_options, **user_input}

# Try to update the config entry with the options
try:
_LOGGER.debug(f"Attempting to update config entry with options: {updated_options}")
await self.hass.config_entries.async_update_entry(
self.config_entry, options=updated_options
)
except Exception as e:
_LOGGER.error(f"Error updating config entry: {e}")
return self.async_abort(reason="update_failed")

# After updating, return to the main page or close the flow
return self.async_create_entry(title="", data=updated_options)

# Show the form with the existing values as defaults
options_schema = vol.Schema({
vol.Required(CONF_COLLECTOR, default=current_options.get(CONF_COLLECTOR, "")): cv.string,
vol.Required(CONF_POSTAL_CODE, default=current_options.get(CONF_POSTAL_CODE, "")): cv.string,
vol.Required(CONF_STREET_NUMBER, default=current_options.get(CONF_STREET_NUMBER, "")): cv.string,
vol.Optional(CONF_SUFFIX, default=current_options.get(CONF_SUFFIX, "")): cv.string,
vol.Optional(CONF_EXCLUDE_PICKUP_TODAY, default=current_options.get(CONF_EXCLUDE_PICKUP_TODAY, True)): cv.boolean,
vol.Optional(CONF_DATE_ISOFORMAT, default=current_options.get(CONF_DATE_ISOFORMAT, False)): cv.boolean,
vol.Optional(CONF_DEFAULT_LABEL, default=current_options.get(CONF_DEFAULT_LABEL, "geen")): cv.string,
vol.Optional(CONF_EXCLUDE_LIST, default=current_options.get(CONF_EXCLUDE_LIST, "")): cv.string,
})

return self.async_show_form(
step_id="init", data_schema=options_schema, errors=errors,
description_placeholders={}
)
2 changes: 1 addition & 1 deletion custom_components/afvalwijzer/const/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

API = "api"
NAME = "afvalwijzer"
VERSION = "2024.12.04"
VERSION = "2024.12.05"

ISSUE_URL = "https://github.com/xirixiz/homeassistant-afvalwijzer/issues"

Expand Down
2 changes: 1 addition & 1 deletion custom_components/afvalwijzer/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/xirixiz/homeassistant-afvalwijzer/issues",
"requirements": [],
"version": "2024.12.04"
"version": "2024.12.05"
}
3 changes: 3 additions & 0 deletions custom_components/afvalwijzer/sensor_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ async def async_update(self):
else:
self._update_attributes_non_date(collection_date)

# Update last_update timestamp
self._last_update = datetime.now().isoformat()

except Exception as err:
_LOGGER.error(f"Error updating custom sensor {self.name}: {err}")
self._handle_value_error()
Expand Down
3 changes: 3 additions & 0 deletions custom_components/afvalwijzer/sensor_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ async def async_update(self):
else:
self._update_attributes_non_date(collection_date)

# Update last_update timestamp
self._last_update = datetime.now().isoformat()

except Exception as err:
_LOGGER.error(f"Error updating sensor {self.name}: {err}")
self._handle_value_error()
Expand Down
17 changes: 11 additions & 6 deletions custom_components/afvalwijzer/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@
# street_number = "22"

# Afvalalert
provider = "afvalalert"
postal_code = "7881NW"
street_number = "4"
# provider = "afvalalert"
# postal_code = "7881NW"
# street_number = "4"

# ACV
# provider = "acv"
# postal_code = "6714KK"
# street_number = "20"

# iCalendar file
# provider = "veldhoven"
# postal_code = "5508SB"
# street_number = "51"
provider = "veldhoven"
postal_code = "5508SB"
street_number = "51"

# Afvalwijzer
# provider = "mijnafvalwijzer"
Expand Down Expand Up @@ -113,6 +113,11 @@
# postal_code = "3446GL"
# street_number = "16"

# Ximmio
# provider = "almere"
# postal_code = "1311HG"
# street_number = "20"

# Ximmio
# provider = "acv"
# postal_code = "6713CG"
Expand Down

0 comments on commit 3ded955

Please sign in to comment.