Skip to content

Commit

Permalink
v0.0.16: adding option for showing units (default no).
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenterheerdt committed May 27, 2020
1 parent d7f7885 commit b94bf3e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 36 deletions.
7 changes: 6 additions & 1 deletion custom_components/smart_irrigation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
CONF_LEAD_TIME,
CONF_MAXIMUM_DURATION,
CONF_FORCE_MODE_DURATION,
CONF_SHOW_UNITS,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -105,10 +106,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
longitude = hass.config.as_dict().get(CONF_LONGITUDE)
elevation = hass.config.as_dict().get(CONF_ELEVATION)

# lead time, max duration, force_mode_duration
# handle options: lead time, max duration, force_mode_duration, show units
lead_time = entry.options.get(CONF_LEAD_TIME, 0)
maximum_duration = entry.options.get(CONF_MAXIMUM_DURATION, -1)
force_mode_duration = entry.options.get(CONF_FORCE_MODE_DURATION, 0)
show_units = entry.options.get(CONF_SHOW_UNITS, False)

# set up coordinator
coordinator = SmartIrrigationUpdateCoordinator(
Expand All @@ -129,6 +131,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
lead_time=lead_time,
maximum_duration=maximum_duration,
force_mode_duration=force_mode_duration,
show_units=show_units,
)

await coordinator.async_refresh()
Expand Down Expand Up @@ -208,6 +211,7 @@ def __init__(
lead_time,
maximum_duration,
force_mode_duration,
show_units,
):
"""Initialize."""
self.api = OWMClient(api_key=api_key, longitude=longitude, latitude=latitude)
Expand All @@ -226,6 +230,7 @@ def __init__(
self.lead_time = lead_time
self.maximum_duration = maximum_duration
self.force_mode_duration = force_mode_duration
self.show_units = show_units
self.platforms = []
self.bucket = 0
self.hass = hass
Expand Down
5 changes: 5 additions & 0 deletions custom_components/smart_irrigation/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
CONF_LEAD_TIME,
CONF_MAXIMUM_DURATION,
CONF_FORCE_MODE_DURATION,
CONF_SHOW_UNITS,
)


Expand Down Expand Up @@ -290,6 +291,10 @@ async def async_step_user(self, user_input=None):
CONF_FORCE_MODE_DURATION,
default=self.options.get(CONF_FORCE_MODE_DURATION, 0),
): int,
vol.Required(
CONF_SHOW_UNITS,
default=self.options.get(CONF_SHOW_UNITS, False),
): bool,
}
),
)
Expand Down
1 change: 1 addition & 0 deletions custom_components/smart_irrigation/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
CONF_FORCE_MODE_DURATION = "force_mode_duration"
CONF_ADJUSTED_RUN_TIME_MINUTES = "adjusted_run_time_minutes"
CONF_BASE_SCHEDULE_INDEX_MINUTES = "base_schedule_index_minutes"
CONF_SHOW_UNITS = "show_units"

# Events
EVENT_BUCKET_UPDATED = "smart_irrigation_bucket_updated_event"
Expand Down
64 changes: 31 additions & 33 deletions custom_components/smart_irrigation/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ async def async_added_to_hass(self):
state = await self.async_get_last_state()
if state is not None and state.state != "unavailable":
self._state = float(state.state)
_LOGGER.info(
"async_added_t_hass type: {} state: {}, attributes: {}".format(
self.type, state.state, state.attributes
)
)
# _LOGGER.info(
# "async_added_t_hass type: {} state: {}, attributes: {}".format(
# self.type, state.state, state.attributes
# )
# )
confs = (
CONF_EVAPOTRANSPIRATION,
CONF_NETTO_PRECIPITATION,
Expand All @@ -124,11 +124,11 @@ async def async_added_to_hass(self):
if a in state.attributes:
try:
a_val = state.attributes[a]
numeric_part, unit = a_val.split(" ")
_LOGGER.info(
"attr: {}, val: {}, unit: {}".format(a, numeric_part, unit)
)
numeric_part = float(numeric_part)
# no split needed if we don't show units
if self.coordinator.show_units:
numeric_part, unit = a_val.split(" ")
else:
numeric_part = float(a_val)
if a in (
CONF_EVAPOTRANSPIRATION,
CONF_NETTO_PRECIPITATION,
Expand Down Expand Up @@ -212,7 +212,6 @@ def update_state(self):
return result["art"]
else:
# 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 @@ -358,126 +357,125 @@ def get_evapotranspiration(self, d):
)
return fao56

def show_liter_or_gallon(self, value, show_unit=False):
def show_liter_or_gallon(self, value):
"""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:
if self.coordinator.show_units:
retval = retval + f" {UNIT_OF_MEASUREMENT_LITERS}"
return retval
else:
retval = f"{round(value * LITER_TO_GALLON_FACTOR,2)}"
if show_unit:
if self.coordinator.show_units:
retval = retval + f" {UNIT_OF_MEASUREMENT_GALLONS}"
return retval

def show_liter_or_gallon_per_minute(self, value, show_unit=False):
def show_liter_or_gallon_per_minute(self, value):
"""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:
if self.coordinator.show_units:
retval = retval + f" {UNIT_OF_MEASUREMENT_LPM}"
return retval
else:
retval = f"{round(value * LITER_TO_GALLON_FACTOR,2)}"
if show_unit:
if self.coordinator.show_units:
retval = retval + f" {UNIT_OF_MEASUREMENT_GPM}"
return retval

def show_mm_or_inch(self, value, show_unit=False):
def show_mm_or_inch(self, value):
"""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:
if self.coordinator.show_units:
retval = retval + f" {UNIT_OF_MEASUREMENT_MMS}"
return retval
else:
if isinstance(value, list):
retval = f"{[round(x * MM_TO_INCH_FACTOR,2) for x in value]}"
else:
retval = f"{round(value * MM_TO_INCH_FACTOR,2)}"
if show_unit:
if self.coordinator.show_units:
retval = retval + f" {UNIT_OF_MEASUREMENT_INCHES}"
return retval

def show_mm_or_inch_per_hour(self, value, show_unit=False):
def show_mm_or_inch_per_hour(self, value):
"""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:
if self.coordinator.show_units:
retval = retval + f" {UNIT_OF_MEASUREMENT_MMS_HOUR}"
return retval
else:
if isinstance(value, list):
retval = f"{[round(x * MM_TO_INCH_FACTOR,2) for x in value]}"
else:
retval = f"{round(value * MM_TO_INCH_FACTOR,2)}"
if show_unit:
if self.coordinator.show_units:
retval = retval + f" {UNIT_OF_MEASUREMENT_INCHES_HOUR}"
return retval

def show_m2_or_sq_ft(self, value, show_unit=False):
def show_m2_or_sq_ft(self, value):
"""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:
if self.coordinator.show_units:
retval = retval + f" {UNIT_OF_MEASUREMENT_M2}"
return retval
else:
retval = f"{round(value * M2_TO_SQ_FT_FACTOR,2)}"
if show_unit:
if self.coordinator.show_units:
retval = retval + f" {UNIT_OF_MEASUREMENT_SQ_FT}"
return retval

def show_percentage(self, value, show_unit=False):
def show_percentage(self, value):
"""Return nicely formatted percentages."""
if value is None:
return "unknown"
value = float(value)
retval = round(value * 100, 2)
if show_unit:
if self.coordinator.show_units:
return f"{retval} %"
else:
return retval

def show_seconds(self, value, show_unit=False):
def show_seconds(self, value):
"""Return nicely formatted seconds."""
if value is None:
return "unknown"
if show_unit:
if self.coordinator.show_units:
return f"{value} s"
else:
return value

def show_minutes(self, value, show_unit=False):
def show_minutes(self, value):
"""Return nicely formatted minutes."""
if value is None:
return "unknown"
value = float(value)
retval = round(value / 60, 2)
if show_unit:
if self.coordinator.show_units:
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
3 changes: 2 additions & 1 deletion custom_components/smart_irrigation/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"data": {
"lead_time": "Lead time, time to add to adjusted run time (seconds).",
"maximum_duration": "Maximum duration, including any lead time (-1 means no maximum).",
"force_mode_duration": "Number of seconds to run irrigation when in force mode."
"force_mode_duration": "Number of seconds to run irrigation when in force mode.",
"show_units": "Show units in entity attributes."
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion custom_components/smart_irrigation/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"data": {
"lead_time": "Lead time, time to add to adjusted run time (seconds).",
"maximum_duration": "Maximum duration, including any lead time (-1 means no maximum).",
"force_mode_duration": "Number of seconds to run irrigation when in force mode."
"force_mode_duration": "Number of seconds to run irrigation when in force mode.",
"show_units": "Show units in entity attributes."
}
}
}
Expand Down

0 comments on commit b94bf3e

Please sign in to comment.