Skip to content

Commit

Permalink
Add cancel service
Browse files Browse the repository at this point in the history
  • Loading branch information
rgc99 committed Apr 11, 2021
1 parent ab92ae0 commit 7383eed
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ The binary sensor associated with each controller and zone provide several servi
- `enable`
- `disable`
- `toggle`
- `cancel`
- `manual_run`
- `adjust_time`

Expand All @@ -285,6 +286,14 @@ Enables/disables/toggles the controller or zone respectively.
| ---------------------- | -------- | ----------- |
| `entity_id` | no | Controller or zone to enable/disable/toggle.

### Service `cancel`

Cancels the current running schedule.

| Service data attribute | Optional | Description |
| ---------------------- | -------- | ----------- |
| `entity_id` | no | Controller or zone to cancel.

### Service `manual_run`

Turn on the controller or zone for a period of time.
Expand Down
1 change: 1 addition & 0 deletions custom_components/irrigation_unlimited/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
SERVICE_ENABLE = "enable"
SERVICE_DISABLE = "disable"
SERVICE_TOGGLE = "toggle"
SERVICE_CANCEL = "cancel"
SERVICE_TIME_ADJUST = "adjust_time"
SERVICE_MANUAL_RUN = "manual_run"

Expand Down
45 changes: 44 additions & 1 deletion custom_components/irrigation_unlimited/irrigation_unlimited.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
CONF_CONFIG,
CONF_TIMELINE,
CONF_ZONE_ID,
SERVICE_CANCEL,
SERVICE_DISABLE,
SERVICE_ENABLE,
SERVICE_TOGGLE,
Expand Down Expand Up @@ -511,12 +512,14 @@ class IURunQueue(list):
RQ_STATUS_REDUCED: int = 0x04
RQ_STATUS_SORTED: int = 0x08
RQ_STATUS_UPDATED: int = 0x10
RQ_STATUS_CANCELED: int = 0x20

def __init__(self) -> None:
# Private variables
self._current_run: IURun = None
self._next_run: IURun = None
self._sorted: bool = False
self._cancel_request: bool = False
return

@property
Expand All @@ -541,6 +544,11 @@ def add(
self._sorted = False
return self

def cancel(self) -> None:
"""Flag the current run to be cancelled"""
self._cancel_request = True
return

def clear_all(self) -> bool:
modified: bool = False
if len(self) > 0:
Expand Down Expand Up @@ -619,6 +627,16 @@ def remove_expired(self, time: datetime) -> bool:
i -= 1
return modified

def remove_current(self) -> bool:
"""Remove the current run"""
modified: bool = False
if self._current_run is not None:
if len(self) > 0:
self.pop(0)
self._current_run = None
modified = True
return modified

def update_queue(self, time: datetime) -> int:
"""Update the run queue. Sort the queue, remove expired runs
and set current and next runs.
Expand All @@ -630,6 +648,11 @@ def update_queue(self, time: datetime) -> int:
if self.sort():
status |= self.RQ_STATUS_SORTED

if self._cancel_request:
if self.remove_current():
status |= self.RQ_STATUS_CANCELED
self._cancel_request = False

if self.remove_expired(time):
status |= self.RQ_STATUS_REDUCED

Expand Down Expand Up @@ -916,6 +939,11 @@ def service_manual_run(self, data: MappingProxyType, time: datetime) -> None:
self._run_queue.add_manual(ns, wash_td(data[CONF_TIME]))
return

def service_cancel(self, data: MappingProxyType, time: datetime) -> None:
"""Cancel the current running schedule"""
self._run_queue.cancel()
return

def add(self, schedule: IUSchedule) -> IUSchedule:
"""Add a new schedule to the zone"""
self._schedules.append(schedule)
Expand Down Expand Up @@ -1465,10 +1493,11 @@ def muster(self, time: datetime, force: bool) -> int:
IURunQueue.RQ_STATUS_CLEARED
| IURunQueue.RQ_STATUS_EXTENDED
| IURunQueue.RQ_STATUS_SORTED
| IURunQueue.RQ_STATUS_CANCELED
)
!= 0
):
all = zone_status & IURunQueue.RQ_STATUS_CLEARED
all = bool(zone_status & (IURunQueue.RQ_STATUS_CLEARED | IURunQueue.RQ_STATUS_CANCELED))
status |= self._run_queue.rebuild_schedule(
time, self._zones, self._preamble, self._postamble, all
)
Expand Down Expand Up @@ -1820,6 +1849,15 @@ def manual_run_all(
zone.service_manual_run(data, time)
return

def cancel_all(
controller: IUController, data: MappingProxyType, time: datetime
) -> None:
zl = data.get(CONF_ZONES, None)
for zone in controller.zones:
if zl is None or zone.zone_index + 1 in zl:
zone.service_cancel(data, time)
return

def notify_children(controller: IUController) -> None:
for zone in controller.zones:
zone.request_update()
Expand All @@ -1843,6 +1881,11 @@ def notify_children(controller: IUController) -> None:
else:
controller.enabled = not controller.enabled
notify_children(controller)
elif service == SERVICE_CANCEL:
if zone is not None:
zone.service_cancel(data, time)
else:
cancel_all(controller, data, time)
elif service == SERVICE_TIME_ADJUST:
if zone is not None:
zone.service_adjust_time(data, time)
Expand Down
6 changes: 6 additions & 0 deletions custom_components/irrigation_unlimited/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .entity import IUEntity
from .const import (
DOMAIN,
SERVICE_CANCEL,
SERVICE_ENABLE,
SERVICE_DISABLE,
SERVICE_TOGGLE,
Expand Down Expand Up @@ -85,12 +86,17 @@ async def async_manual_run(entity: IUEntity, call: ServiceCall) -> None:
return


async def async_cancel(entity: IUEntity, call: ServiceCall) -> None:
entity.dispatch(SERVICE_CANCEL, call)


def register_platform_services(platform: entity_platform.EntityPlatform) -> None:
platform.async_register_entity_service(SERVICE_ENABLE, ENTITY_SCHEMA, async_enable)
platform.async_register_entity_service(
SERVICE_DISABLE, ENTITY_SCHEMA, async_disable
)
platform.async_register_entity_service(SERVICE_TOGGLE, ENTITY_SCHEMA, async_toggle)
platform.async_register_entity_service(SERVICE_CANCEL, ENTITY_SCHEMA, async_cancel)
platform.async_register_entity_service(
SERVICE_TIME_ADJUST, TIME_ADJUST_SCHEMA, async_time_adjust
)
Expand Down
6 changes: 6 additions & 0 deletions custom_components/irrigation_unlimited/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ toggle:
entity_id:
description: Name of the Irrigation Unlimited entity.
example: 'binary_sensor.irrigation_unlimited_c1_z1'
cancel:
description: Cancel the current run.
fields:
entity_id:
description: Name of the Irrigation Unlimited entity.
example: 'binary_sensor.irrigation_unlimited_c1_z1'
reload:
description: Reload the configuration
fields:
Expand Down

0 comments on commit 7383eed

Please sign in to comment.