1 """Rachio smart hose timer calendar."""
3 from datetime
import datetime, timedelta
20 DOMAIN
as DOMAIN_RACHIO,
32 KEY_TOTAL_RUN_DURATION,
35 from .coordinator
import RachioScheduleUpdateCoordinator
36 from .device
import RachioPerson
38 _LOGGER = logging.getLogger(__name__)
43 config_entry: ConfigEntry,
44 async_add_entities: AddEntitiesCallback,
46 """Set up entry for Rachio smart hose timer calendar."""
47 person: RachioPerson = hass.data[DOMAIN_RACHIO][config_entry.entry_id]
50 for base_station
in person.base_stations
55 CoordinatorEntity[RachioScheduleUpdateCoordinator], CalendarEntity
57 """Rachio calendar entity."""
59 _attr_has_entity_name =
True
60 _attr_translation_key =
"calendar"
61 _attr_supported_features = CalendarEntityFeature.DELETE_EVENT
64 self, coordinator: RachioScheduleUpdateCoordinator, base_station
66 """Initialize a Rachio calendar entity."""
69 self._event: CalendarEvent |
None =
None
70 self.
_location_location = coordinator.base_station[KEY_ADDRESS][KEY_LOCALITY]
72 "base": coordinator.base_station[KEY_SERIAL_NUMBER]
74 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.base_station[KEY_ID]}-calendar"
78 def event(self) -> CalendarEvent | None:
79 """Return the next upcoming event."""
82 start_time = dt_util.parse_datetime(event[KEY_START_TIME], raise_on_error=
True)
84 [event[KEY_VALVE_NAME]
for event
in event[KEY_RUN_SUMMARIES]]
87 summary=event[KEY_PROGRAM_NAME],
88 start=dt_util.as_local(start_time),
89 end=dt_util.as_local(start_time)
96 """Handle current or next event."""
101 start_time = dt_util.parse_datetime(
102 self.
_previous_event_previous_event[KEY_START_TIME], raise_on_error=
True
107 if start_time <= dt_util.now() <= end_time:
110 schedule = iter(self.coordinator.data)
111 event = next(schedule,
None)
115 not event[KEY_SKIPPABLE]
or KEY_SKIP
in event[KEY_RUN_SUMMARIES][0]
117 event = next(schedule,
None)
124 self, hass: HomeAssistant, start_date: datetime, end_date: datetime
125 ) -> list[CalendarEvent]:
126 """Get all events in a specific time frame."""
127 if not self.coordinator.data:
129 schedule = self.coordinator.data
130 event_list: list[CalendarEvent] = []
133 event_start = dt_util.as_local(
134 dt_util.parse_datetime(run[KEY_START_TIME], raise_on_error=
True)
136 if event_start > end_date:
138 if run[KEY_SKIPPABLE]:
140 seconds=
int(run[KEY_TOTAL_RUN_DURATION])
144 seconds=
int(run[KEY_RUN_SUMMARIES][0][KEY_DURATION_SECONDS])
148 event_end > start_date
149 and event_start < end_date
150 and KEY_SKIP
not in run[KEY_RUN_SUMMARIES][0]
153 [event[KEY_VALVE_NAME]
for event
in run[KEY_RUN_SUMMARIES]]
156 summary=run[KEY_PROGRAM_NAME],
161 uid=f
"{run[KEY_PROGRAM_ID]}/{run[KEY_START_TIME]}",
163 event_list.append(event)
169 recurrence_id: str |
None =
None,
170 recurrence_range: str |
None =
None,
172 """Skip an upcoming event on the calendar."""
173 program, timestamp = uid.split(
"/")
list[CalendarEvent] async_get_events(self, HomeAssistant hass, datetime start_date, datetime end_date)
_attr_translation_placeholders
None __init__(self, RachioScheduleUpdateCoordinator coordinator, base_station)
dict[str, Any]|None _handle_upcoming_event(self)
None async_delete_event(self, str uid, str|None recurrence_id=None, str|None recurrence_range=None)
CalendarEvent|None event(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)