Skip to content

Commit

Permalink
Change to helper, refactor config_flow
Browse files Browse the repository at this point in the history
  • Loading branch information
bruxy70 committed Jul 8, 2022
1 parent 4df2b63 commit b5acff0
Show file tree
Hide file tree
Showing 14 changed files with 371 additions and 453 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

# Garbage Collection

The `garbage_collection` component is a Home Assistant integration that creates a custom sensor for monitoring a regular garbage collection schedule. The sensor can be configured for a number of different patterns:
The `garbage_collection` component is a Home Assistant helper that creates a custom sensor for monitoring a regular garbage collection schedule. The sensor can be configured for a number of different patterns:

- `weekly` schedule (including multiple collection days, e.g. on Tuesday and Thursday)
- `every-n-weeks` repeats every `period` of weeks, starting from the week number `first_week`. It uses the week number - therefore, it restarts each year, as the weeks start from number 1 each year.
Expand Down Expand Up @@ -54,18 +54,18 @@ These are some examples using this sensor. The Lovelace config examples are incl
into the `custom_components` directory of your Home Assistant
installation.
3. Restart Home Assistant.
4. Configure the `garbage_collection` sensor.
4. Configure the `garbage_collection` helper.

### INSTALLATION VIA Home Assistant Community Store (HACS)

1. Ensure that [HACS](https://hacs.xyz/) is installed.
2. Search for and install the "Garbage Collection" integration.
3. Restart Home Assistant.
4. Configure the `garbage_collection` sensor.
4. Configure the `garbage_collection` helper.

## Configuration

Go to `Configuration`/`Devices & Services`, click on the `+ ADD INTEGRATION` button, select `Garbage Collection` and configure the integration.<br />If you would like to add more than one collection schedule, click on the `+ ADD INTEGRATION` button again and add another `Garbage Collection` integration instance.
Go to `Settings`/`Devices & Services`/`Helpers`, click on the `+ CREATE HELPER` button, select `Garbage Collection` and configure the helper.<br />If you would like to add more than one collection schedule, click on the `+ CREATE HELPER` button again and add another `Garbage Collection` helper instance.

**The configuration hapend in 2 steps.** In the first step, you select the `frequency` and common parameters. In the second step you configure additional parameters depending on the selected frequency.

Expand Down Expand Up @@ -150,7 +150,7 @@ The monthly schedule has two flavors: it can trigger either on the **n<sup>th</s

There are a couple of **blueprints**, automatically moving the collection falling on a public holiday. Or if there was a public holiday in the week before the scheduled collection.

The Public Holidays **blueprints** use a separate custom integration **Holidays**, available through **HACS**, that you can configure for different countries.
The Public Holidays **blueprints** use a separate custom helper **Holidays**, available through **HACS**, that you can configure for different countries.

| <!-- --> | <!-- --> |
| :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -217,7 +217,7 @@ sensor:
### Monthly on a fixed date
This will create a schedule on a fixed date each month. For example on the 3rd each month. The integration does not allow it, as it is generally designed around paterns evolving around weekly schedules (since garbage collection typically happens on a set day in a week, rather than set day in a month). But few of you wanted that, so here you go.
This will create a schedule on a fixed date each month. For example on the 3rd each month. The helper does not allow it, as it is generally designed around paterns evolving around weekly schedules (since garbage collection typically happens on a set day in a week, rather than set day in a month). But few of you wanted that, so here you go.
[![Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.](https://my.home-assistant.io/badges/blueprint_import.svg)](https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https%3A%2F%2Fgithub.com%2Fbruxy70%2FGarbage-Collection%2Fblob%2Fmaster%2Fblueprints%2Fmonthly_fixed_date.yaml) One fixed date
Expand Down
50 changes: 40 additions & 10 deletions custom_components/garbage_collection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
from datetime import timedelta
from types import MappingProxyType
from typing import Any
from typing import Any, Dict

import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util
Expand Down Expand Up @@ -256,10 +256,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
_LOGGER.debug(
"Setting %s (%s) from ConfigFlow",
config_entry.title,
config_entry.data[const.CONF_FREQUENCY],
config_entry.options[const.CONF_FREQUENCY],
)
# Backward compatibility - clean-up (can be removed later?)
config_entry.options = MappingProxyType({})
config_entry.add_update_listener(update_listener)
# Add sensor
hass.async_create_task(
Expand Down Expand Up @@ -288,6 +286,8 @@ async def async_migrate_entry(_, config_entry: ConfigEntry) -> bool:
_LOGGER.info(
"Migrating %s from version %s", config_entry.title, config_entry.version
)
new_data: Dict[str, Any]
new_options: Dict[str, Any]
new_data = {**config_entry.data}
new_options = {**config_entry.options}
removed_data: dict[str, Any] = {}
Expand Down Expand Up @@ -343,6 +343,41 @@ async def async_migrate_entry(_, config_entry: ConfigEntry) -> bool:
new_options[const.CONF_WEEKDAY_ORDER_NUMBER] = list(
map(str, new_options[const.CONF_WEEKDAY_ORDER_NUMBER])
)
if config_entry.version <= 5:
for conf in [
const.CONF_FREQUENCY,
const.CONF_ICON_NORMAL,
const.CONF_ICON_TODAY,
const.CONF_ICON_TOMORROW,
const.CONF_MANUAL,
const.CONF_OFFSET,
const.CONF_EXPIRE_AFTER,
const.CONF_VERBOSE_STATE,
const.CONF_FIRST_MONTH,
const.CONF_LAST_MONTH,
const.CONF_COLLECTION_DAYS,
const.CONF_WEEKDAY_ORDER_NUMBER,
const.CONF_FORCE_WEEK_NUMBERS,
const.CONF_WEEK_ORDER_NUMBER,
const.CONF_DATE,
const.CONF_PERIOD,
const.CONF_FIRST_WEEK,
const.CONF_FIRST_DATE,
const.CONF_SENSORS,
const.CONF_VERBOSE_FORMAT,
const.CONF_DATE_FORMAT,
]:
if conf in new_data:
new_options[conf] = new_data.get(conf)
del new_data[conf]
if (
const.CONF_EXPIRE_AFTER in new_options
and len(new_options[const.CONF_EXPIRE_AFTER]) == 5
):
new_options[const.CONF_EXPIRE_AFTER] = (
new_options[const.CONF_EXPIRE_AFTER] + ":00"
)

config_entry.version = const.CONFIG_VERSION
config_entry.data = MappingProxyType({**new_data})
config_entry.options = MappingProxyType({**new_options})
Expand All @@ -367,12 +402,7 @@ async def async_migrate_entry(_, config_entry: ConfigEntry) -> bool:


async def update_listener(hass, entry):
"""Update listener."""
# The OptionsFlow saves data to options.
# Move them back to data and clean options (dirty, but not sure how else to do that)
if len(entry.options) > 0:
entry.data = entry.options
entry.options = {}
"""Update listener - to re-create device after options update."""
await hass.config_entries.async_forward_entry_unload(entry, const.SENSOR_PLATFORM)
hass.async_add_job(
hass.config_entries.async_forward_entry_setup(entry, const.SENSOR_PLATFORM)
Expand Down
Loading

0 comments on commit b5acff0

Please sign in to comment.