Skip to content

Commit

Permalink
Add watering plan data to zone switch attributes for non smart wateri…
Browse files Browse the repository at this point in the history
…ng configurations

Closes #5
  • Loading branch information
sebr committed Feb 20, 2020
1 parent 0367e4a commit 2ba1e59
Showing 1 changed file with 60 additions and 22 deletions.
82 changes: 60 additions & 22 deletions custom_components/bhyve/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
ATTR_SPRINKLER_TYPE = "sprinkler_type"
ATTR_IMAGE_URL = "image_url"
ATTR_STARTED_WATERING_AT = "started_watering_station_at"
ATTR_WATERING_PROGRAM = "watering_program"
ATTR_PROGRAM_NAME = "program_name"
ATTR_SMART_WATERING_PLAN = "watering_program"

ATTR_PROGRAM = "program_{}"


async def async_setup_platform(hass, config, async_add_entities, _discovery_info=None):
Expand Down Expand Up @@ -119,36 +120,73 @@ def _set_watering_started(self, timestamp):

def _set_watering_program(self, program):
if program is None:
self._attrs[ATTR_WATERING_PROGRAM] = None
return

program_name = program.get("name", "Unknown")
program_id = program.get("program")
enabled = program.get("enabled", False)
if enabled is False:
program_enabled = program.get("enabled", False)
program_attr = ATTR_PROGRAM.format(program_id)

# Filter out any run times which are not for this switch
active_program_run_times = list(
filter(
lambda x: (x.get("station") == self._zone_id),
program.get("run_times", []),
)
)

is_smart_program = program.get("is_smart_program", False)

self._attrs[program_attr] = {
"enabled": program_enabled,
"name": program_name,
"is_smart_program": is_smart_program,
}

if not program_enabled or not active_program_run_times:
_LOGGER.info(
"Watering program {} ({}) is not enabled, skipping".format(
program_name, program_id
)
)
if is_smart_program == True:
self._attrs[ATTR_SMART_WATERING_PLAN] = None

return

watering_plan = program.get("watering_plan", [])
upcoming_run_times = []
for plan in watering_plan:
run_times = plan.get("run_times")
if run_times:
zone_times = list(
filter(lambda x: (x.get("station") == self._zone_id), run_times)
)
if zone_times:
plan_date = orbit_time_to_local_time(plan.get("date"))
for time in plan.get("start_times", []):
t = dt.parse_time(time)
upcoming_run_times.append(
plan_date + timedelta(hours=t.hour, minutes=t.minute)
)
self._attrs[ATTR_WATERING_PROGRAM] = upcoming_run_times
"""
"name": "Backyard",
"frequency": { "type": "days", "days": [1, 4] },
"start_times": ["07:30"],
"budget": 100,
"program": "a",
"run_times": [{ "run_time": 20, "station": 1 }],
"""

if is_smart_program == True:
upcoming_run_times = []
for plan in program.get("watering_plan", []):
run_times = plan.get("run_times")
if run_times:
zone_times = list(
filter(lambda x: (x.get("station") == self._zone_id), run_times)
)
if zone_times:
plan_date = orbit_time_to_local_time(plan.get("date"))
for time in plan.get("start_times", []):
t = dt.parse_time(time)
upcoming_run_times.append(
plan_date + timedelta(hours=t.hour, minutes=t.minute)
)
self._attrs[ATTR_SMART_WATERING_PLAN] = upcoming_run_times
else:
self._attrs[program_attr].update(
{
"start_times": program.get("start_times", []),
"frequency": program.get("frequency", []),
"run_times": active_program_run_times,
}
)

def _on_ws_data(self, data):
"""
Expand Down Expand Up @@ -183,7 +221,7 @@ def _on_ws_data(self, data):
if lifecycle_phase != "destroy":
self._set_watering_program(watering_program)
else:
self._attrs[ATTR_WATERING_PROGRAM] = None
self._attrs[ATTR_SMART_WATERING_PLAN] = None

async def _send_station_message(self, station_payload):
try:
Expand Down

0 comments on commit 2ba1e59

Please sign in to comment.