Skip to content

Commit

Permalink
feat: Moving from Attributes to Sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
robbrad committed Oct 20, 2024
1 parent 40aba33 commit 36b8158
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
!wiki
!wiki/**/*
!custom_components
!custom_components/**/*/
__pycache__
!TO_BE_CONVERTED
!.devcontainer
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ This integration can be installed directly via HACS. To install:
1. Restart your Home Assistant.
1. In the Home Assistant UI go to `Settings` > `Devices & Services` click `+ Add Integration` and search for `UK Bin Collection Data`.

### Overriding the Bin Icon and Bin Colour
We realise it is difficult to set a colour from the councils text for the Bin Type and to keep the integration generic we dont capture colour from a council(not all councils supply this as a field), only bin type and next collection date.

When you configure the componenent on the first screen you can set a JSON string to map the bin type to the colour and icon

Here is an example to set the colour and icon for the type `Empty Standard General Waste`. This type is the type returned from the council for the bin. You can do this for multiple bins.

If you miss this on the first setup you can reconfigure it.

```
{
"Empty Standard General Waste":
{
"icon": "mdi:trash-can",
"color": "blue"
}
}
---
## Standalone Usage
Expand Down
19 changes: 7 additions & 12 deletions custom_components/uk_bin_collection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up UK Bin Collection Data from a config entry."""
_LOGGER.info(LOG_PREFIX + "Data Supplied: %s", entry.data)
council_name = entry.data.get("council", "unknown council")
_LOGGER.info(
LOG_PREFIX + "Setting up UK Bin Collection Data for council: %s", council_name
)

council_name = entry.data.get("council", "Unknown Council")
_LOGGER.info(LOG_PREFIX + "Setting up UK Bin Collection Data for council: %s", council_name)

hass.data.setdefault(DOMAIN, {})

Expand All @@ -23,18 +22,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

_LOGGER.info(LOG_PREFIX + "Config entry data: %s", entry.data)

async def _async_finish_startup(_):
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

hass.async_create_task(_async_finish_startup(None))
# Forward the entry setup to the sensor platform
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

_LOGGER.info(
LOG_PREFIX + "Successfully set up UK Bin Collection Data for council: %s",
council_name,
)
_LOGGER.info(LOG_PREFIX + "Successfully set up UK Bin Collection Data for council: %s", council_name)
return True



async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
57 changes: 44 additions & 13 deletions custom_components/uk_bin_collection/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import json

import aiohttp
import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -88,11 +89,20 @@ async def async_step_user(self, user_input=None):
]

if user_input is not None:
# Perform validation and setup here based on user_input
if user_input["name"] is None or user_input["name"] == "":
errors["base"] = "name"
if user_input["council"] is None or user_input["council"] == "":
errors["base"] = "council"

# Validate the JSON mapping only if provided
if user_input.get("icon_color_mapping"):
try:
json.loads(user_input["icon_color_mapping"])
except json.JSONDecodeError:
errors["icon_color_mapping"] = "invalid_json"

# Check for errors
if not errors:
user_input["council"] = self.council_names[
self.council_options.index(user_input["council"])
Expand All @@ -101,15 +111,14 @@ async def async_step_user(self, user_input=None):
_LOGGER.info(LOG_PREFIX + "User input: %s", user_input)
return await self.async_step_council()

_LOGGER.info(
LOG_PREFIX + "Showing user form with options: %s", self.council_options
)
# Show the form
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required("name", default=""): cv.string,
vol.Required("council", default=""): vol.In(self.council_options),
vol.Optional("icon_color_mapping", default=""): cv.string, # Optional field
}
),
errors=errors,
Expand Down Expand Up @@ -178,16 +187,29 @@ async def async_step_reconfigure_confirm(
user_input["council"] = self.council_names[
self.council_options.index(user_input["council"])
]
# Update the config entry with the new data
data = {**existing_data, **user_input}
self.hass.config_entries.async_update_entry(
self.config_entry,
title=user_input.get("name", self.config_entry.title),
data=data,
)
# Optionally, reload the integration to apply changes
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
return self.async_abort(reason="Reconfigure Successful")

# Validate the icon and color mapping JSON if provided
if user_input.get("icon_color_mapping"):
try:
json.loads(user_input["icon_color_mapping"])
except json.JSONDecodeError:
errors["icon_color_mapping"] = "invalid_json"

if not errors:
# Merge the user input with existing data
data = {**existing_data, **user_input}

# Ensure icon_color_mapping is properly updated
data["icon_color_mapping"] = user_input.get("icon_color_mapping", "")

self.hass.config_entries.async_update_entry(
self.config_entry,
title=user_input.get("name", self.config_entry.title),
data=data,
)
# Optionally, reload the integration to apply changes
await self.hass.config_entries.async_reload(self.config_entry.entry_id)
return self.async_abort(reason="Reconfigure Successful")

# Get the council schema based on the current council setting
council_schema = await self.get_council_schema(council_key)
Expand Down Expand Up @@ -253,6 +275,15 @@ async def async_step_reconfigure_confirm(
)
added_fields.add("timeout")

# Add the icon_color_mapping field with a default value if it exists
schema = schema.extend(
{
vol.Optional(
"icon_color_mapping", default=existing_data.get("icon_color_mapping", "")
): str
}
)

# Add any other fields defined in council_schema that haven't been added yet
for key, field in council_schema.schema.items():
if key not in added_fields:
Expand Down
Loading

0 comments on commit 36b8158

Please sign in to comment.