support sensor event_index

This commit is contained in:
5ila5
2023-08-20 19:57:37 +02:00
parent 19d2209b54
commit c362580ae6
2 changed files with 19 additions and 1 deletions

View File

@@ -29,6 +29,7 @@ CONF_LEADTIME = "leadtime"
CONF_DATE_TEMPLATE = "date_template"
CONF_COLLECTION_TYPES = "types"
CONF_ADD_DAYS_TO = "add_days_to"
CONF_EVENT_INDEX = "event_index"
class DetailsFormat(Enum):
@@ -52,6 +53,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_DATE_TEMPLATE): cv.template,
vol.Optional(CONF_ADD_DAYS_TO, default=False): cv.boolean,
vol.Optional(CONF_EVENT_INDEX, default=0): cv.positive_int,
}
)
@@ -88,6 +90,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
value_template=value_template,
date_template=date_template,
add_days_to=config.get(CONF_ADD_DAYS_TO),
event_index=config.get(CONF_EVENT_INDEX),
)
)
@@ -110,6 +113,7 @@ class ScheduleSensor(SensorEntity):
value_template,
date_template,
add_days_to,
event_index,
):
"""Initialize the entity."""
self._api = api
@@ -121,6 +125,7 @@ class ScheduleSensor(SensorEntity):
self._value_template = value_template
self._date_template = date_template
self._add_days_to = add_days_to
self._event_index = event_index
self._value = None
@@ -201,6 +206,7 @@ class ScheduleSensor(SensorEntity):
count=1,
include_types=self._collection_types,
include_today=self._include_today,
start_index=self._event_index,
)
self._set_state(upcoming1)
@@ -220,6 +226,7 @@ class ScheduleSensor(SensorEntity):
leadtime=self._leadtime,
include_types=self._collection_types,
include_today=self._include_today,
start_index=self._event_index,
)
for collection in upcoming:
attributes[self._render_date(collection)] = self._separator.join(
@@ -229,7 +236,10 @@ class ScheduleSensor(SensorEntity):
# show list of collections in details
for t in collection_types:
collections = self._aggregator.get_upcoming(
count=1, include_types=[t], include_today=self._include_today
count=1,
include_types=[t],
include_today=self._include_today,
start_index=self._event_index,
)
date = (
"" if len(collections) == 0 else self._render_date(collections[0])

View File

@@ -33,6 +33,7 @@ class CollectionAggregator:
include_types=None,
exclude_types=None,
include_today=False,
start_index=None,
):
"""Return list of all entries, limited by count and/or leadtime.
@@ -47,6 +48,7 @@ class CollectionAggregator:
include_types=include_types,
exclude_types=exclude_types,
include_today=include_today,
start_index=start_index,
)
def get_upcoming_group_by_day(
@@ -56,6 +58,7 @@ class CollectionAggregator:
include_types=None,
exclude_types=None,
include_today=False,
start_index=None,
):
"""Return list of all entries, grouped by day, limited by count and/or leadtime."""
entries = []
@@ -73,6 +76,8 @@ class CollectionAggregator:
for key, group in iterator:
entries.append(CollectionGroup.create(list(group)))
if start_index is not None:
entries = entries[start_index:]
if count is not None:
entries = entries[:count]
@@ -86,6 +91,7 @@ class CollectionAggregator:
include_types=None,
exclude_types=None,
include_today=False,
start_index=None,
):
# remove unwanted waste types from include list
if include_types is not None:
@@ -115,6 +121,8 @@ class CollectionAggregator:
entries.sort(key=lambda e: e.date)
# remove surplus entries
if start_index is not None:
entries = entries[start_index:]
if count is not None:
entries = entries[:count]