Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/components/rachio/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
KEY_ZONE_NUMBER = "zoneNumber"
KEY_ZONES = "zones"
KEY_SCHEDULES = "scheduleRules"
KEY_FLEX_SCHEDULES = "flexScheduleRules"
KEY_SCHEDULE_ID = "scheduleId"
KEY_CUSTOM_SHADE = "customShade"
KEY_CUSTOM_CROP = "customCrop"
Expand Down
8 changes: 7 additions & 1 deletion homeassistant/components/rachio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
KEY_DEVICES,
KEY_ENABLED,
KEY_EXTERNAL_ID,
KEY_FLEX_SCHEDULES,
KEY_ID,
KEY_MAC_ADDRESS,
KEY_MODEL,
Expand Down Expand Up @@ -92,6 +93,7 @@ def __init__(self, hass, rachio, data, webhooks):
self.model = data[KEY_MODEL]
self._zones = data[KEY_ZONES]
self._schedules = data[KEY_SCHEDULES]
self._flex_schedules = data[KEY_FLEX_SCHEDULES]
self._init_data = data
self._webhooks = webhooks
_LOGGER.debug('%s has ID "%s"', str(self), self.controller_id)
Expand Down Expand Up @@ -177,9 +179,13 @@ def get_zone(self, zone_id) -> Optional[dict]:
return None

def list_schedules(self) -> list:
"""Return a list of schedules."""
"""Return a list of fixed schedules."""
return self._schedules

def list_flex_schedules(self) -> list:
"""Return a list of flex schedules."""
return self._flex_schedules

def stop_watering(self) -> None:
"""Stop watering all zones connected to this controller."""
self.rachio.device.stopWater(self.controller_id)
Expand Down
30 changes: 13 additions & 17 deletions homeassistant/components/rachio/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging

from homeassistant.components.switch import SwitchDevice
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect

from .const import (
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -178,7 +179,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]
Expand Down Expand Up @@ -295,21 +295,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."""
Expand All @@ -318,7 +313,7 @@ def name(self) -> str:
@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:
Expand All @@ -331,7 +326,7 @@ def device_state_attributes(self) -> dict:
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
Expand All @@ -342,7 +337,7 @@ def schedule_is_enabled(self) -> bool:
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,
)
Expand All @@ -354,13 +349,14 @@ def turn_off(self, **kwargs) -> None:
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:
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare Apr 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A coroutine function can't be a callback.

  • Please remove the async in async def.
  • Rename the function to _async_handle_update.
  • Change self.schedule_update_ha_state to self.async_write_ha_state inside.
  • Do this for all the switch classes and the abstract method.
  • Rename _handle_any_update too, similarly.
  • Decorate all these methods with @callback.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MartinHjelmare

@brg468 Do you want to do a PR for this?, or I can take care of it needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you had any other changes you were planning go ahead, if not I can do it in a bit.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on fixing something in homekit so you'll probably get to it before I do.

"""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 [
Expand Down