Skip to content

Commit

Permalink
Merge pull request #164 from disforw/main
Browse files Browse the repository at this point in the history
Adding service to begin watering a program
  • Loading branch information
sebr authored Jan 11, 2023
2 parents b6d4c00 + b5c1b13 commit f0e40a1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
7 changes: 7 additions & 0 deletions custom_components/bhyve/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ set_smart_watering_soil_moisture:
percentage:
description: Moisture level between 0 - 100 (percent)
example: 50

start_program:
description: Begin watering a program
fields:
entity_id:
description: Program
example: "switch.front_yard_program"
30 changes: 29 additions & 1 deletion custom_components/bhyve/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
SERVICE_STOP_WATERING = "stop_watering"
SERVICE_SET_MANUAL_PRESET_RUNTIME = "set_manual_preset_runtime"
SERVICE_SET_SMART_WATERING_SOIL_MOISTURE = "set_smart_watering_soil_moisture"
SERVICE_START_PROGRAM = "start_program"


SERVICE_TO_METHOD = {
Expand All @@ -125,6 +126,10 @@
"method": "set_smart_watering_soil_moisture",
"schema": SET_SMART_WATERING_SOIL_MOISTURE_SCHEMA,
},
SERVICE_START_PROGRAM: {
"method": "start_program",
"schema": SERVICE_BASE_SCHEMA,
},
}


Expand Down Expand Up @@ -275,6 +280,30 @@ async def async_turn_on(self, **kwargs: Any) -> None:
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off."""
await self._set_state(False)

async def start_program(self):
"""Begins running a program."""
program_payload = self._program["program"]
await self._send_program_message(program_payload)

async def _send_program_message(self, station_payload):
try:
now = datetime.datetime.now()
iso_time = now.strftime("%Y-%m-%dT%H:%M:%SZ")

payload = {
"event": EVENT_CHANGE_MODE,
"mode": "manual",
"device_id": self._device_id,
"timestamp": iso_time,
"program": station_payload,
}
_LOGGER.debug(payload)
await self._bhyve.send_message(payload)

except BHyveError as err:
_LOGGER.warning("Failed to send to BHyve websocket message %s", err)
raise (err)

async def async_added_to_hass(self):
"""Register callbacks."""
Expand Down Expand Up @@ -608,7 +637,6 @@ async def start_watering(self, minutes):
station_payload = [{"station": self._zone_id, "run_time": minutes}]
self._is_on = True
await self._send_station_message(station_payload)

async def stop_watering(self):
"""Turns off the switch and stops watering."""
station_payload = []
Expand Down

0 comments on commit f0e40a1

Please sign in to comment.