Skip to content

Commit

Permalink
Merge pull request #40 from mampfes/add_days_to
Browse files Browse the repository at this point in the history
add daysTo to device state attributes
  • Loading branch information
mampfes authored Apr 1, 2021
2 parents 3e0719a + 82a0fcb commit c9977cd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ sensor:
leadtime: LEADTIME
value_template: VALUE_TEMPLATE
date_template: DATE_TEMPLATE
add_days_to: ADD_DAYS_TO
types:
- Waste Type 1
- Waste Type 2
Expand Down Expand Up @@ -288,6 +289,12 @@ Template string used to format collection dates within the more-info popup.

See [Template Variables](#template-variables) for a list of available variables.

**add_days_to**

*(boolean) (optional, default: ```False```)*

Adds an attribute with the label `daysTo` and the number of days to the next collection to the entity state of the source.

**types**

*(list of strings) (optional)*
Expand Down Expand Up @@ -507,6 +514,13 @@ entity: sensor.garbage
type: 'custom:garbage-collection-card'
```

### 13. How can I sort waste type specific entities?

Prerequisites: You already have dedicated sensors per waste type and want to show the sensor with the next collection in a Lovelace card.

Add `add_days_to: True` to the configuration of all sensors you want to sort. This will add the attribute `daysTo` which can be used by e.g. [auto-entities](https://github.com/thomasloven/lovelace-auto-entities) to sort entities by day of next collection.


## How to add new sources

1. Create a new source in folder `custom_components/waste_collection_schedule/package/source` with the lower case url of your service provider (e.g. `abc_com.py` for `http://www.abc.com`).
Expand Down
19 changes: 13 additions & 6 deletions custom_components/waste_collection_schedule/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
CONF_LEADTIME = "leadtime"
CONF_DATE_TEMPLATE = "date_template"
CONF_COLLECTION_TYPES = "types"
CONF_ADD_DAYS_TO = "add_days_to"


class DetailsFormat(Enum):
Expand All @@ -42,6 +43,7 @@ class DetailsFormat(Enum):
vol.Optional(CONF_COLLECTION_TYPES): cv.ensure_list,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_DATE_TEMPLATE): cv.template,
vol.Optional(CONF_ADD_DAYS_TO, default=False): cv.boolean,
}
)

Expand Down Expand Up @@ -69,6 +71,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
collection_types=config.get(CONF_COLLECTION_TYPES),
value_template=value_template,
date_template=date_template,
add_days_to=config.get(CONF_ADD_DAYS_TO),
)
)

Expand All @@ -90,6 +93,7 @@ def __init__(
collection_types,
value_template,
date_template,
add_days_to,
):
"""Initialize the entity."""
self._api = api
Expand All @@ -101,6 +105,7 @@ def __init__(
self._collection_types = collection_types
self._value_template = value_template
self._date_template = date_template
self._add_days_to = add_days_to

self._state = STATE_UNKNOWN
self._icon = None
Expand Down Expand Up @@ -205,14 +210,12 @@ def _update_sensor(self):
_LOGGER.error(f"source_index {self._source_index} out of range")
return None

self._set_state(
self._scraper.get_upcoming_group_by_day(
count=1,
types=self._collection_types,
include_today=self._include_today,
)
upcoming1 = self._scraper.get_upcoming_group_by_day(
count=1, types=self._collection_types, include_today=self._include_today,
)

self._set_state(upcoming1)

attributes = {}

collection_types = (
Expand Down Expand Up @@ -257,6 +260,10 @@ def _update_sensor(self):
refreshtime = self._scraper.refreshtime.isoformat(timespec="seconds")
attributes["last_update"] = refreshtime

if len(upcoming1) > 0:
if self._add_days_to:
attributes["daysTo"] = upcoming1[0].daysTo

self._attributes = attributes
self._add_refreshtime()

Expand Down

0 comments on commit c9977cd

Please sign in to comment.