Add Rachio Flex Schedules (#33533)

* Add Rachio Flex Schedules

* Remove Duration Property

* Missed duration call

* Black formatting
This commit is contained in:
Brian Rogers 2020-04-02 14:43:40 -04:00 committed by GitHub
parent 39408ab240
commit 4caf65dc97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 18 deletions

View file

@ -4,6 +4,7 @@ from datetime import timedelta
import logging
from homeassistant.components.switch import SwitchDevice
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import (
@ -68,13 +69,13 @@ def _create_entities(hass, config_entry):
entities.append(RachioStandbySwitch(controller))
zones = controller.list_zones()
schedules = controller.list_schedules()
flex_schedules = controller.list_flex_schedules()
current_schedule = controller.current_schedule
for zone in zones:
_LOGGER.debug("Rachio setting up zone: %s", zone)
entities.append(RachioZone(person, controller, zone, current_schedule))
for sched in schedules:
_LOGGER.debug("Added schedule: %s", sched)
for sched in schedules + flex_schedules:
entities.append(RachioSchedule(person, controller, sched, current_schedule))
_LOGGER.debug("Added %s", entities)
return entities
@ -180,7 +181,6 @@ class RachioZone(RachioSwitch):
def __init__(self, person, controller, data, current_schedule):
"""Initialize a new Rachio Zone."""
self._id = data[KEY_ID]
_LOGGER.debug("zone_data: %s", data)
self._zone_name = data[KEY_NAME]
self._zone_number = data[KEY_ZONE_NUMBER]
self._zone_enabled = data[KEY_ENABLED]
@ -297,21 +297,16 @@ class RachioSchedule(RachioSwitch):
def __init__(self, person, controller, data, current_schedule):
"""Initialize a new Rachio Schedule."""
self._id = data[KEY_ID]
self._schedule_id = data[KEY_ID]
self._schedule_name = data[KEY_NAME]
self._duration = data[KEY_DURATION]
self._schedule_enabled = data[KEY_ENABLED]
self._summary = data[KEY_SUMMARY]
self._current_schedule = current_schedule
super().__init__(controller, poll=False)
self._state = self.schedule_id == self._current_schedule.get(KEY_SCHEDULE_ID)
self._state = self._schedule_id == self._current_schedule.get(KEY_SCHEDULE_ID)
self._undo_dispatcher = None
@property
def schedule_id(self) -> str:
"""How the Rachio API refers to the schedule."""
return self._id
@property
def name(self) -> str:
"""Return the friendly name of the schedule."""
@ -320,7 +315,7 @@ class RachioSchedule(RachioSwitch):
@property
def unique_id(self) -> str:
"""Return a unique id by combining controller id and schedule."""
return f"{self._controller.controller_id}-schedule-{self.schedule_id}"
return f"{self._controller.controller_id}-schedule-{self._schedule_id}"
@property
def icon(self) -> str:
@ -333,7 +328,7 @@ class RachioSchedule(RachioSwitch):
return {
ATTR_SCHEDULE_SUMMARY: self._summary,
ATTR_SCHEDULE_ENABLED: self.schedule_is_enabled,
ATTR_SCHEDULE_DURATION: self._duration / 60,
ATTR_SCHEDULE_DURATION: f"{round(self._duration / 60)} minutes",
}
@property
@ -344,7 +339,7 @@ class RachioSchedule(RachioSwitch):
def turn_on(self, **kwargs) -> None:
"""Start this schedule."""
self._controller.rachio.schedulerule.start(self.schedule_id)
self._controller.rachio.schedulerule.start(self._schedule_id)
_LOGGER.debug(
"Schedule %s started on %s", self.name, self._controller.name,
)
@ -356,13 +351,14 @@ class RachioSchedule(RachioSwitch):
def _poll_update(self, data=None) -> bool:
"""Poll the API to check whether the schedule is running."""
self._current_schedule = self._controller.current_schedule
return self.schedule_id == self._current_schedule.get(KEY_SCHEDULE_ID)
return self._schedule_id == self._current_schedule.get(KEY_SCHEDULE_ID)
def _handle_update(self, *args, **kwargs) -> None:
@callback
async def _handle_update(self, *args, **kwargs) -> None:
"""Handle incoming webhook schedule data."""
# Schedule ID not passed when running individual zones, so we catch that error
try:
if args[0][KEY_SCHEDULE_ID] == self.schedule_id:
if args[0][KEY_SCHEDULE_ID] == self._schedule_id:
if args[0][KEY_SUBTYPE] in [SUBTYPE_SCHEDULE_STARTED]:
self._state = True
elif args[0][KEY_SUBTYPE] in [