Skip to content

Commit

Permalink
v0.014 - fixing issue #16
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenterheerdt committed May 27, 2020
1 parent 7138c67 commit 533e4c0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 67 deletions.
3 changes: 2 additions & 1 deletion custom_components/smart_irrigation/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
DOMAIN = "smart_irrigation"
NAME = "Smart Irrigation"
DOMAIN_DATA = f"{DOMAIN}_data"
VERSION = "0.0.13"
VERSION = "0.0.14"

ISSUE_URL = "https://github.com/jeroenterheerdt/HASmartIrrigation/issues"

Expand Down Expand Up @@ -83,6 +83,7 @@

# UNITS
UNIT_OF_MEASUREMENT_SECONDS = "s"
UNIT_OF_MEASUREMENT_MINUTES = "min"
UNIT_OF_MEASUREMENT_UNKNOWN = "unknown"
UNIT_OF_MEASUREMENT_LITERS = "l"
UNIT_OF_MEASUREMENT_GALLONS = "gallon"
Expand Down
143 changes: 77 additions & 66 deletions custom_components/smart_irrigation/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
UNIT_OF_MEASUREMENT_MMS_HOUR,
UNIT_OF_MEASUREMENT_GPM,
UNIT_OF_MEASUREMENT_LPM,
UNIT_OF_MEASUREMENT_MINUTES,
CONF_LEAD_TIME,
CONF_MAXIMUM_DURATION,
CONF_ADJUSTED_RUN_TIME_MINUTES,
Expand Down Expand Up @@ -101,73 +102,69 @@ async def async_added_to_hass(self):
)

state = await self.async_get_last_state()
if state is not None:
if state is not None and state.state != "unavailable":
self._state = float(state.state)
# _LOGGER.info(
# "async_added_t_hass state: {}, attributes: {}".format(
# state.state, state.attributes
# )
# )
for a in state.attributes:
if a in (
# CONF_AREA,
# CONF_FLOW,
# CONF_NUMBER_OF_SPRINKLERS,
# CONF_PEAK_ET,
# CONF_PRECIPITATION_RATE,
# CONF_REFERENCE_ET,
# CONF_THROUGHPUT,
CONF_EVAPOTRANSPIRATION,
CONF_NETTO_PRECIPITATION,
CONF_PRECIPITATION,
CONF_RAIN,
CONF_SNOW,
CONF_WATER_BUDGET,
CONF_BUCKET,
):
_LOGGER.info(
"async_added_t_hass type: {} state: {}, attributes: {}".format(
self.type, state.state, state.attributes
)
)
confs = (
CONF_EVAPOTRANSPIRATION,
CONF_NETTO_PRECIPITATION,
CONF_PRECIPITATION,
CONF_RAIN,
CONF_SNOW,
CONF_WATER_BUDGET,
CONF_BUCKET,
CONF_ADJUSTED_RUN_TIME_MINUTES,
)
for a in confs:
if a in state.attributes:
try:
a_val = state.attributes[a]
if isinstance(a_val, str) and not a_val.startswith("["):
numeric_part, unit = a_val.split(" ")
# _LOGGER.info(
# "attr: {}, val: {}, unit: {}".format(
# a, numeric_part, unit
# )
# )
numeric_part = float(numeric_part)
if (
unit in ("%", UNIT_OF_MEASUREMENT_SECONDS)
or self.coordinator.system_of_measurement
== SETTING_METRIC
):
setattr(self, a, numeric_part)
else:
if unit in (
UNIT_OF_MEASUREMENT_INCHES,
UNIT_OF_MEASUREMENT_INCHES_HOUR,
):
setattr(
self,
a,
round(numeric_part / MM_TO_INCH_FACTOR, 2),
)
elif unit in (
UNIT_OF_MEASUREMENT_GALLONS,
UNIT_OF_MEASUREMENT_GPM,
):
setattr(
self,
a,
round(numeric_part / LITER_TO_GALLON_FACTOR, 2),
)
elif unit in UNIT_OF_MEASUREMENT_SQ_FT:
setattr(
self,
a,
round(numeric_part / M2_TO_SQ_FT_FACTOR, 2),
)
else:
setattr(self, a, numeric_part)
numeric_part, unit = a_val.split(" ")
_LOGGER.info(
"attr: {}, val: {}, unit: {}".format(a, numeric_part, unit)
)
numeric_part = float(numeric_part)
if a in (
CONF_EVAPOTRANSPIRATION,
CONF_NETTO_PRECIPITATION,
CONF_PRECIPITATION,
CONF_RAIN,
CONF_SNOW,
CONF_BUCKET,
):
if a == CONF_EVAPOTRANSPIRATION:
self.evapotranspiration = numeric_part
elif a == CONF_NETTO_PRECIPITATION:
self.bucket_delta = numeric_part
elif a == CONF_PRECIPITATION:
self.precipitation = numeric_part
elif a == CONF_RAIN:
self.rain = numeric_part
elif a == CONF_SNOW:
self.snow = numeric_part
elif a == CONF_BUCKET:
self.bucket = numeric_part
self.coordinator.bucket = self.bucket
factor = 1
if self.coordinator.system_of_measurement != SETTING_METRIC:
factor = MM_TO_INCH_FACTOR
# unit should be included here as well
setattr(
self,
a,
f"{round(numeric_part / MM_TO_INCH_FACTOR, 2)}",
)
elif a in (CONF_WATER_BUDGET, CONF_ADJUSTED_RUN_TIME_MINUTES):
# no need for conversion here
if a == CONF_WATER_BUDGET:
self.water_budget = numeric_part
# no need to store adjusted run time minutes
setattr(self, a, f"{numeric_part}")

except Exception as e:
_LOGGER.error(e)

Expand Down Expand Up @@ -197,22 +194,24 @@ def update_state(self):
"""Update the state."""
if self.type == TYPE_BASE_SCHEDULE_INDEX:
return round(self.coordinator.base_schedule_index, 1)
# hourly adjusted run time
elif self.type == TYPE_CURRENT_ADJUSTED_RUN_TIME:
data = self.coordinator.data["daily"][0]
# parse precipitation out of the today data
self.precipitation = self.get_precipitation(data)
# calculate et out of the today data
self.evapotranspiration = self.get_evapotranspiration(data)
# calculate the adjusted runtime!

self.bucket_delta = self.precipitation - self.evapotranspiration

result = self.calculate_water_budget_and_adjusted_run_time(
self.bucket_delta
)
self.water_budget = result["wb"]
return result["art"]
else:
# adjusted run time
# daily adjusted run time
_LOGGER.warning("calling calculate for daily art")
result = self.calculate_water_budget_and_adjusted_run_time(
self.coordinator.bucket
)
Expand Down Expand Up @@ -281,6 +280,9 @@ def get_precipitation(self, data):
self.snow = float(data["snow"])
_LOGGER.info("rain: {}, snow: {}".format(self.rain, self.snow))
retval = self.rain + self.snow
if retval.count(".") > 1:
retval = retval.split(".")[0] + "." + retval.split(".")[1]
retval = float(retval)
return retval

def estimate_fao56_daily(
Expand Down Expand Up @@ -357,6 +359,7 @@ def show_liter_or_gallon(self, value, show_unit=True):
"""Return nicely formatted liters or gallons."""
if value is None:
return "unknown"
value = float(value)
if self.coordinator.system_of_measurement == SETTING_METRIC:
retval = f"{value}"
if show_unit:
Expand All @@ -372,6 +375,7 @@ def show_liter_or_gallon_per_minute(self, value, show_unit=True):
"""Return nicely formatted liters or gallons."""
if value is None:
return "unknown"
value = float(value)
if self.coordinator.system_of_measurement == SETTING_METRIC:
retval = f"{value}"
if show_unit:
Expand All @@ -387,6 +391,8 @@ def show_mm_or_inch(self, value, show_unit=True):
"""Return nicely formatted mm or inches."""
if value is None:
return "unknown"
if not isinstance(value, list):
value = float(value)
if self.coordinator.system_of_measurement == SETTING_METRIC:
retval = f"{value}"
if show_unit:
Expand All @@ -405,6 +411,7 @@ def show_mm_or_inch_per_hour(self, value, show_unit=True):
"""Return nicely formatted mm or inches per hour."""
if value is None:
return "unknown"
value = float(value)
if self.coordinator.system_of_measurement == SETTING_METRIC:
retval = f"{value}"
if show_unit:
Expand All @@ -423,6 +430,7 @@ def show_m2_or_sq_ft(self, value, show_unit=True):
"""Return nicely formatted m2 or sq ft."""
if value is None:
return "unknown"
value = float(value)
if self.coordinator.system_of_measurement == SETTING_METRIC:
retval = f"{value}"
if show_unit:
Expand All @@ -438,6 +446,7 @@ def show_percentage(self, value, show_unit=True):
"""Return nicely formatted percentages."""
if value is None:
return "unknown"
value = float(value)
retval = round(value * 100, 2)
if show_unit:
return f"{retval} %"
Expand All @@ -457,13 +466,15 @@ def show_minutes(self, value, show_unit=True):
"""Return nicely formatted minutes."""
if value is None:
return "unknown"
value = float(value)
retval = round(value / 60, 2)
if show_unit:
return f"{retval} min"
else:
return retval

def calculate_water_budget_and_adjusted_run_time(self, bucket_val):
_LOGGER.warning("calc wb and art called with bucket={}".format(bucket_val))
water_budget = 0
adjusted_run_time = 0
if bucket_val is None or bucket_val >= 0:
Expand Down

0 comments on commit 533e4c0

Please sign in to comment.