forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add calendar to Husqvarna Automower (home-assistant#120775)
* Add Calendar * update * change timezone for tests * fix requirements * bump aioautomower to 2024.6.3b0 * bump aioautomower to 2024.6.4b0 * fix req * align dates * adjust * nnbw * better * improvements * req * update requirements * tests * tweaks * shift functions to library * tests * bump to aioautomower==2024.9.0b1 * tests * remove ZoneInfo wrapper * use timetzone from start_date object * Update requirements_all.txt * Fix names in ProgramEvent
- Loading branch information
1 parent
089c942
commit fccbaa0
Showing
8 changed files
with
412 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"""Creates a calendar entity for the mower.""" | ||
|
||
from datetime import datetime | ||
import logging | ||
|
||
from homeassistant.components.calendar import CalendarEntity, CalendarEvent | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import HomeAssistantError | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
from homeassistant.util import dt as dt_util | ||
|
||
from . import AutomowerConfigEntry | ||
from .coordinator import AutomowerDataUpdateCoordinator | ||
from .entity import AutomowerBaseEntity | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
entry: AutomowerConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up lawn mower platform.""" | ||
coordinator = entry.runtime_data | ||
async_add_entities( | ||
AutomowerCalendarEntity(mower_id, coordinator) for mower_id in coordinator.data | ||
) | ||
|
||
|
||
class AutomowerCalendarEntity(AutomowerBaseEntity, CalendarEntity): | ||
"""Representation of the Automower Calendar element.""" | ||
|
||
_attr_name: str | None = None | ||
|
||
def __init__( | ||
self, | ||
mower_id: str, | ||
coordinator: AutomowerDataUpdateCoordinator, | ||
) -> None: | ||
"""Set up AutomowerCalendarEntity.""" | ||
super().__init__(mower_id, coordinator) | ||
self._attr_unique_id = mower_id | ||
self._event: CalendarEvent | None = None | ||
|
||
@property | ||
def event(self) -> CalendarEvent | None: | ||
"""Return the current or next upcoming event.""" | ||
schedule = self.mower_attributes.calendar | ||
if schedule.timeline is None: | ||
return None | ||
cursor = schedule.timeline.active_after(dt_util.now()) | ||
program_event = next(cursor, None) | ||
_LOGGER.debug("program_event %s", program_event) | ||
if not program_event: | ||
return None | ||
return CalendarEvent( | ||
summary=program_event.schedule_name, | ||
start=program_event.start.replace(tzinfo=dt_util.DEFAULT_TIME_ZONE), | ||
end=program_event.end.replace(tzinfo=dt_util.DEFAULT_TIME_ZONE), | ||
rrule=program_event.rrule_str, | ||
) | ||
|
||
async def async_get_events( | ||
self, hass: HomeAssistant, start_date: datetime, end_date: datetime | ||
) -> list[CalendarEvent]: | ||
"""Return calendar events within a datetime range. | ||
This is only called when opening the calendar in the UI. | ||
""" | ||
schedule = self.mower_attributes.calendar | ||
if schedule.timeline is None: | ||
raise HomeAssistantError("Unable to get events: No schedule set") | ||
cursor = schedule.timeline.overlapping( | ||
start_date, | ||
end_date, | ||
) | ||
return [ | ||
CalendarEvent( | ||
summary=program_event.schedule_name, | ||
start=program_event.start.replace(tzinfo=start_date.tzinfo), | ||
end=program_event.end.replace(tzinfo=start_date.tzinfo), | ||
rrule=program_event.rrule_str, | ||
) | ||
for program_event in cursor | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
tests/components/husqvarna_automower/snapshots/test_calendar.ambr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# serializer version: 1 | ||
# name: test_calendar_snapshot[start_date0-end_date0] | ||
dict({ | ||
'calendar.test_mower_1': dict({ | ||
'events': list([ | ||
dict({ | ||
'end': '2023-06-05T09:00:00+02:00', | ||
'start': '2023-06-05T01:00:00+02:00', | ||
'summary': 'Back lawn schedule 2', | ||
}), | ||
dict({ | ||
'end': '2023-06-05T10:00:00+02:00', | ||
'start': '2023-06-05T02:00:00+02:00', | ||
'summary': 'Schedule 1', | ||
}), | ||
dict({ | ||
'end': '2023-06-06T00:00:00+02:00', | ||
'start': '2023-06-05T19:00:00+02:00', | ||
'summary': 'Front lawn schedule 1', | ||
}), | ||
dict({ | ||
'end': '2023-06-06T08:00:00+02:00', | ||
'start': '2023-06-06T00:00:00+02:00', | ||
'summary': 'Back lawn schedule 1', | ||
}), | ||
dict({ | ||
'end': '2023-06-06T08:00:00+02:00', | ||
'start': '2023-06-06T00:00:00+02:00', | ||
'summary': 'Front lawn schedule 2', | ||
}), | ||
dict({ | ||
'end': '2023-06-06T09:00:00+02:00', | ||
'start': '2023-06-06T01:00:00+02:00', | ||
'summary': 'Back lawn schedule 2', | ||
}), | ||
dict({ | ||
'end': '2023-06-08T00:00:00+02:00', | ||
'start': '2023-06-07T19:00:00+02:00', | ||
'summary': 'Front lawn schedule 1', | ||
}), | ||
dict({ | ||
'end': '2023-06-08T08:00:00+02:00', | ||
'start': '2023-06-08T00:00:00+02:00', | ||
'summary': 'Back lawn schedule 1', | ||
}), | ||
dict({ | ||
'end': '2023-06-08T08:00:00+02:00', | ||
'start': '2023-06-08T00:00:00+02:00', | ||
'summary': 'Front lawn schedule 2', | ||
}), | ||
dict({ | ||
'end': '2023-06-08T09:00:00+02:00', | ||
'start': '2023-06-08T01:00:00+02:00', | ||
'summary': 'Back lawn schedule 2', | ||
}), | ||
dict({ | ||
'end': '2023-06-08T10:00:00+02:00', | ||
'start': '2023-06-08T02:00:00+02:00', | ||
'summary': 'Schedule 1', | ||
}), | ||
dict({ | ||
'end': '2023-06-10T00:00:00+02:00', | ||
'start': '2023-06-09T19:00:00+02:00', | ||
'summary': 'Front lawn schedule 1', | ||
}), | ||
dict({ | ||
'end': '2023-06-10T08:00:00+02:00', | ||
'start': '2023-06-10T00:00:00+02:00', | ||
'summary': 'Front lawn schedule 2', | ||
}), | ||
dict({ | ||
'end': '2023-06-10T08:00:00+02:00', | ||
'start': '2023-06-10T00:00:00+02:00', | ||
'summary': 'Back lawn schedule 1', | ||
}), | ||
dict({ | ||
'end': '2023-06-10T09:00:00+02:00', | ||
'start': '2023-06-10T01:00:00+02:00', | ||
'summary': 'Back lawn schedule 2', | ||
}), | ||
dict({ | ||
'end': '2023-06-10T10:00:00+02:00', | ||
'start': '2023-06-10T02:00:00+02:00', | ||
'summary': 'Schedule 1', | ||
}), | ||
]), | ||
}), | ||
}) | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.