All my configs for home assistant
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bhyve_next_watering.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. now = dt_util.now()
  2. zone_entity_id = data.get("entity_id")
  3. zone = hass.states.get(zone_entity_id)
  4. device_name = zone.attributes["device_name"]
  5. logger.info("updating next_watering for zone: ({}: {})".format(device_name, zone))
  6. next_watering_entity = f"sensor.next_watering_{device_name}"
  7. next_watering_attrs = {
  8. "friendly_name": f"{device_name} next watering"
  9. }
  10. rain_delay_finishing_entity = f"sensor.rain_delay_finishing_{device_name}"
  11. rain_delay_finishing_attrs = {
  12. "friendly_name": f"{device_name} rain delay finishing"
  13. }
  14. rain_delay = hass.states.get(f"switch.rain_delay_{device_name}")
  15. if zone.state == "unavailable":
  16. hass.states.set(next_watering_entity, "Unavailable", next_watering_attrs)
  17. hass.states.set(rain_delay_finishing_entity, "Unavailable", rain_delay_finishing_attrs)
  18. else:
  19. delay_finishes_at = None
  20. next_watering = None
  21. if rain_delay.state == "on":
  22. started_at = dt_util.as_timestamp(rain_delay.attributes.get("started_at"))
  23. delay_seconds = rain_delay.attributes.get("delay") * 3600
  24. delay_finishes_at = dt_util.as_local(dt_util.utc_from_timestamp(started_at + delay_seconds))
  25. hass.states.set(rain_delay_finishing_entity, delay_finishes_at, rain_delay_finishing_attrs)
  26. else:
  27. hass.states.set(rain_delay_finishing_entity, None, rain_delay_finishing_attrs)
  28. for timestamp in (zone.attributes.get('watering_program', []) or []):
  29. watering_time = dt_util.parse_datetime(str(timestamp))
  30. if watering_time > now and (delay_finishes_at is None or watering_time > delay_finishes_at):
  31. next_watering = watering_time
  32. break
  33. hass.states.set(next_watering_entity, next_watering, next_watering_attrs)