Skip to content

Commit

Permalink
Add config options
Browse files Browse the repository at this point in the history
  • Loading branch information
wernerhp committed Nov 28, 2022
1 parent a0f78c8 commit b6e146c
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 116 deletions.
32 changes: 26 additions & 6 deletions custom_components/load_shedding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,37 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up LoadShedding as config entry."""
api_key: str = entry.data.get(CONF_API_KEY)
sepush: SePush = SePush(token=api_key)
if not hass.data.get(DOMAIN):
hass.data.setdefault(DOMAIN, {})

sepush: SePush = None
if api_key := entry.options.get(CONF_API_KEY):
sepush: SePush = SePush(token=api_key)
if not sepush:
return False

stage_coordinator = LoadSheddingStageCoordinator(hass, sepush)
stage_coordinator.update_interval = timedelta(
seconds=entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
)
await stage_coordinator.async_config_entry_first_refresh()

area_coordinator = LoadSheddingAreaCoordinator(
hass, sepush, stage_coordinator=stage_coordinator
)
area_coordinator.update_interval = timedelta(
seconds=entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
)
for conf in entry.options.get(CONF_AREAS, []).values():
for conf in entry.options.get(CONF_AREAS, {}).values():
area = Area(
id=conf.get(CONF_ID),
name=conf.get(CONF_NAME),
)
area_coordinator.add_area(area)
await area_coordinator.async_config_entry_first_refresh()
if not area_coordinator.areas:
return False

quota_coordinator = LoadSheddingQuotaCoordinator(hass, sepush)
quota_coordinator.update_interval = timedelta(seconds=QUOTA_UPDATE_INTERVAL)
await quota_coordinator.async_config_entry_first_refresh()

hass.data[DOMAIN][entry.entry_id] = {
ATTR_STAGE: stage_coordinator,
Expand All @@ -99,6 +102,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
}

entry.async_on_unload(entry.add_update_listener(update_listener))

await stage_coordinator.async_config_entry_first_refresh()
await area_coordinator.async_config_entry_first_refresh()
await quota_coordinator.async_config_entry_first_refresh()
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True
Expand Down Expand Up @@ -126,6 +133,19 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
"""Migrate old entry."""
_LOGGER.debug("Migrating from version %s", config_entry.version)

if config_entry.version == 3:
old_data = {**config_entry.data}
old_options = {**config_entry.options}
new_data = {}
new_options = {
CONF_API_KEY: old_data.get(CONF_API_KEY),
CONF_AREAS: old_options.get(CONF_AREAS, {}),
}
config_entry.version = 4
hass.config_entries.async_update_entry(
config_entry, data=new_data, options=new_options
)

_LOGGER.info("Migration to version %s successful", config_entry.version)
return True

Expand Down
27 changes: 25 additions & 2 deletions custom_components/load_shedding/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
ATTR_START_TIME,
DOMAIN,
NAME,
CONF_MULTI_STAGE_EVENTS,
)


Expand All @@ -34,7 +35,13 @@ async def async_setup_entry(
coordinators = hass.data.get(DOMAIN, {}).get(entry.entry_id)
area_coordinator = coordinators.get(ATTR_AREA)

entities: list[Entity] = [LoadSheddingForecastCalendar(area_coordinator)]
multi_stage_events = False
if entry.options.get(CONF_MULTI_STAGE_EVENTS):
multi_stage_events = True

entities: list[Entity] = [
LoadSheddingForecastCalendar(area_coordinator, multi_stage_events)
]
async_add_entities(entities)


Expand All @@ -43,7 +50,9 @@ class LoadSheddingForecastCalendar(
):
"""Define a LoadShedding Calendar entity."""

def __init__(self, coordinator: CoordinatorEntity) -> None:
def __init__(
self, coordinator: CoordinatorEntity, multi_stage_events: bool
) -> None:
super().__init__(coordinator)
self.data = self.coordinator.data

Expand All @@ -52,6 +61,7 @@ def __init__(self, coordinator: CoordinatorEntity) -> None:
)
self._event: CalendarEvent | None = None
self.entity_id = f"{DOMAIN}.{DOMAIN}_forecast"
self.multi_stage_events = multi_stage_events

@property
def name(self) -> str | None:
Expand Down Expand Up @@ -93,6 +103,19 @@ async def async_get_events(
)
events.append(event)

if not self.multi_stage_events:
continue

# Multi-stage events
for i, cur in enumerate(events):
if i + 1 >= len(events):
continue
nxt = events[i + 1]
if cur.end == nxt.start:
cur.summary = f"{cur.summary}/{nxt.summary}"
cur.end = nxt.end
del events[i + 1]

if events:
self._event = events[0]

Expand Down
Loading

0 comments on commit b6e146c

Please sign in to comment.