Merge pull request #476 from mampfes/fix_global_calendar

fix global calendar
This commit is contained in:
Steffen Zimmermann
2022-12-17 13:44:01 +01:00
committed by GitHub
4 changed files with 50 additions and 58 deletions

View File

@@ -371,7 +371,7 @@ Create a dedicated calendar for this type.
*(string) (optional, default: ```None```)*
Optional title of the dedicated calendar. If not set, the default of the source will be used.
Optional title of the dedicated calendar. If not set, the waste type will be used.
## 2. Add sensor(s) to a source
@@ -686,6 +686,14 @@ Prerequisites: You already have dedicated sensors per waste type and want to sho
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.
### 14. How can I disable the calendar?
If you don't like the calendar provided by Waste Collection Schedule or you have configured some dedicated calendars per waste type and therefore don't need the global calendar any more, you can disable it so that it doesn't show up in the Calendar Dashboard any more:
Go to `Settings` --> `Entities` and select the calendar entity provided by Waste Collection Schedule. Now disable it using the menu items.
[![entities](https://my.home-assistant.io/badges/entities.svg)](https://my.home-assistant.io/redirect/entities/)
## How to add new sources
1. Create a new source in folder `custom_components/waste_collection_schedule/waste_collection_schedule/source` with the lower case url of your service provider (e.g. `abc_com.py` for `http://www.abc.com`).

View File

@@ -23,50 +23,39 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
for scraper in api.scrapers:
dedicated_calendar_types = scraper.get_dedicated_calendar_types()
global_calendar_types = scraper.get_global_calendar_types()
if dedicated_calendar_types is not None:
for type in dedicated_calendar_types:
unique_id = calc_unique_calendar_id(scraper, type)
entities.append(
WasteCollectionCalendar(
api,
scraper,
scraper.get_calendar_title_for_type(type),
[scraper.get_collection_type(type)],
unique_id,
)
)
if global_calendar_types is not None or dedicated_calendar_types is None:
unique_id = calc_unique_calendar_id(scraper)
for type in dedicated_calendar_types:
entities.append(
WasteCollectionCalendar(
api,
scraper,
scraper.calendar_title,
[
scraper.get_collection_type(type)
for type in global_calendar_types
]
if global_calendar_types is not None
else None,
unique_id,
api=api,
scraper=scraper,
name=scraper.get_calendar_title_for_type(type),
include_types={scraper.get_collection_type(type)},
unique_id=calc_unique_calendar_id(scraper, type),
)
)
entities.append(
WasteCollectionCalendar(
api=api,
scraper=scraper,
name=scraper.calendar_title,
exclude_types={scraper.get_collection_type(type) for type in dedicated_calendar_types},
unique_id=calc_unique_calendar_id(scraper),
)
)
async_add_entities(entities)
class WasteCollectionCalendar(CalendarEntity):
"""Calendar entity class."""
def __init__(self, api, scraper, name, types, unique_id: str):
def __init__(self, api, scraper, name, unique_id: str, include_types=None, exclude_types=None):
self._api = api
self._scraper = scraper
self._name = name
self._types = types
self._include_types = include_types
self._exclude_types = exclude_types
self._unique_id = unique_id
self._attr_unique_id = unique_id
@@ -79,7 +68,7 @@ class WasteCollectionCalendar(CalendarEntity):
def event(self):
"""Return next collection event."""
collections = self._scraper.get_upcoming(
count=1, include_today=True, types=self._types
count=1, include_today=True, include_types=self._include_types, exclude_types=self._exclude_types
)
if len(collections) == 0:
@@ -94,7 +83,7 @@ class WasteCollectionCalendar(CalendarEntity):
events = []
for collection in self._scraper.get_upcoming(
include_today=True, types=self._types
include_today=True, include_types=self._include_types, exclude_types=self._exclude_types
):
event = self._convert(collection)

View File

@@ -186,7 +186,7 @@ class ScheduleSensor(SensorEntity):
return None
upcoming1 = self._scraper.get_upcoming_group_by_day(
count=1, types=self._collection_types, include_today=self._include_today,
count=1, include_types=self._collection_types, include_today=self._include_today,
)
self._set_state(upcoming1)
@@ -204,7 +204,7 @@ class ScheduleSensor(SensorEntity):
upcoming = self._scraper.get_upcoming_group_by_day(
count=self._count,
leadtime=self._leadtime,
types=self._collection_types,
include_types=self._collection_types,
include_today=self._include_today,
)
for collection in upcoming:
@@ -215,7 +215,7 @@ class ScheduleSensor(SensorEntity):
# show list of collections in details
for t in collection_types:
collections = self._scraper.get_upcoming(
count=1, types=[t], include_today=self._include_today
count=1, include_types=[t], include_today=self._include_today
)
date = (
"" if len(collections) == 0 else self._render_date(collections[0])
@@ -227,7 +227,7 @@ class ScheduleSensor(SensorEntity):
attributes["upcoming"] = self._scraper.get_upcoming(
count=self._count,
leadtime=self._leadtime,
types=self._collection_types,
include_types=self._collection_types,
include_today=self._include_today,
)
refreshtime = ""

View File

@@ -172,18 +172,9 @@ class Scraper:
if customize.show and customize.use_dedicated_calendar:
types.add(key)
return types or None
return types
def get_global_calendar_types(self):
types = set()
for key, customize in self._customize.items():
if customize.show and not customize.use_dedicated_calendar:
types.add(key)
return types or None
def get_upcoming(self, count=None, leadtime=None, types=None, include_today=False):
def get_upcoming(self, count=None, leadtime=None, include_types=None, exclude_types=None, include_today=False):
"""Return list of all entries, limited by count and/or leadtime.
Keyword arguments:
@@ -194,12 +185,13 @@ class Scraper:
self._entries,
count=count,
leadtime=leadtime,
types=types,
include_types=include_types,
exclude_types=exclude_types,
include_today=include_today,
)
def get_upcoming_group_by_day(
self, count=None, leadtime=None, types=None, include_today=False
self, count=None, leadtime=None, include_types=None, exclude_types=None, include_today=False
):
"""Return list of all entries, grouped by day, limited by count and/or leadtime."""
entries = []
@@ -208,7 +200,8 @@ class Scraper:
self._filter(
self._entries,
leadtime=leadtime,
types=types,
include_types=include_types,
exclude_types=exclude_types,
include_today=include_today,
),
lambda e: e.date,
@@ -226,7 +219,7 @@ class Scraper:
if c is not None and c.dedicated_calendar_title:
return c.dedicated_calendar_title
return self.calendar_title
return self.get_collection_type(type)
def get_collection_type(self, type):
c = self._customize.get(type)
@@ -236,13 +229,15 @@ class Scraper:
return type
def _filter(
self, entries, count=None, leadtime=None, types=None, include_today=False
self, entries, count=None, leadtime=None, include_types=None, exclude_types=None, include_today=False
):
# remove unwanted waste types
if types is not None:
# generate set
types_set = {t for t in types}
entries = list(filter(lambda e: e.type in types_set, self._entries))
# remove unwanted waste types from include list
if include_types is not None:
entries = list(filter(lambda e: e.type in set(include_types), self._entries))
# remove unwanted waste types from exclude list
if exclude_types is not None:
entries = list(filter(lambda e: e.type not in set(exclude_types), self._entries))
# remove expired entries
now = datetime.datetime.now().date()