Skip to content

Commit

Permalink
v0.0.9: configuration split in 3 steps.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenterheerdt committed May 19, 2020
1 parent 23ea60d commit f212f17
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 51 deletions.
2 changes: 1 addition & 1 deletion custom_components/smart_irrigation/OWMClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, api_key, latitude, longitude):
self.url = OWM_URL.format(latitude, longitude, api_key)

def get_data(self):
"""Return data."""
"""Return data."""
try:
r = requests.get(self.url)
d = json.loads(r.text)
Expand Down
16 changes: 1 addition & 15 deletions custom_components/smart_irrigation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
area = entry.data.get(CONF_AREA)
flow = entry.data.get(CONF_FLOW)
number_of_spinklers = entry.data.get(CONF_NUMBER_OF_SPRINKLERS)

reference_et = [
entry.data.get(CONF_REFERENCE_ET_1),
entry.data.get(CONF_REFERENCE_ET_2),
entry.data.get(CONF_REFERENCE_ET_3),
entry.data.get(CONF_REFERENCE_ET_4),
entry.data.get(CONF_REFERENCE_ET_5),
entry.data.get(CONF_REFERENCE_ET_6),
entry.data.get(CONF_REFERENCE_ET_7),
entry.data.get(CONF_REFERENCE_ET_8),
entry.data.get(CONF_REFERENCE_ET_9),
entry.data.get(CONF_REFERENCE_ET_10),
entry.data.get(CONF_REFERENCE_ET_11),
entry.data.get(CONF_REFERENCE_ET_12),
]
reference_et = entry.data.get(CONF_REFERENCE_ET)
reference_et = [float(x) for x in reference_et]

# convert values to internal metric representation if required.
Expand Down
124 changes: 109 additions & 15 deletions custom_components/smart_irrigation/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,33 @@ class SmartIrrigationConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN)

def __init__(self):
"""Initialize."""
self._api_key = None
self._reference_et = {}
self._errors = {}

async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
async def async_step_step3(self, user_input=None):
"""Handle a flow step3."""
self._errors = {}

# only a single instance is allowed
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

if user_input is not None:
user_input[CONF_API_KEY] = self._api_key
user_input[CONF_REFERENCE_ET] = self._reference_et
return self.async_create_entry(title=NAME, data=user_input)
return await self._show_config_form(user_input)

async def async_step_step2(self, user_input=None):
"""Handle a flow step2."""
self._errors = {}

# only a single instance is allowed
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

if user_input is not None:
valid_api = await self._test_api_key(user_input[CONF_API_KEY])
reference_et = [
user_input[CONF_REFERENCE_ET_1],
user_input[CONF_REFERENCE_ET_2],
Expand All @@ -70,25 +85,61 @@ async def async_step_user(self, user_input=None):
user_input[CONF_REFERENCE_ET_11],
user_input[CONF_REFERENCE_ET_12],
]

valid_et = self._check_reference_et(reference_et)
if valid_et:
# store entered values
self._reference_et = reference_et
# show next step
return await self._show_step3(user_input)
else:
self._errors["base"] = "reference_evapotranspiration_problem"
return await self._show_config_form(user_input)
# valid_et = self._check_reference_et(reference_et)

# if valid_api and valid_et:
# return self.async_create_entry(title=NAME, data=user_input)
# if not valid_api:
# self._errors["base"] = "auth"
# elif not valid_et:
# self._errors["base"] = "reference_evapotranspiration_problem"
# return await self._show_config_form(user_input)
return await self._show_config_form(user_input)

if valid_api and valid_et:
return self.async_create_entry(title=NAME, data=user_input)
if not valid_api:
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
self._errors = {}

# only a single instance is allowed
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

if user_input is not None:
valid_api = await self._test_api_key(user_input[CONF_API_KEY])
if valid_api:
# store values entered
self._api_key = user_input[CONF_API_KEY].strip()
# show next step
return await self._show_step2(user_input)
else:
self._errors["base"] = "auth"
elif not valid_et:
self._errors["base"] = "reference_evapotranspiration_problem"
return await self._show_config_form(user_input)
return await self._show_config_form(user_input)
# valid_et = self._check_reference_et(reference_et)

# if valid_api and valid_et:
# return self.async_create_entry(title=NAME, data=user_input)
# if not valid_api:
# self._errors["base"] = "auth"
# elif not valid_et:
# self._errors["base"] = "reference_evapotranspiration_problem"
# return await self._show_config_form(user_input)
return await self._show_config_form(user_input)

async def _show_config_form(self, user_input):
"""Show the configuration form to edit info."""
async def _show_step2(self, user_input):
"""Show the configuration form step 2: reference ET values."""
return self.async_show_form(
step_id="user",
step_id="step2",
data_schema=vol.Schema(
{
vol.Required(CONF_API_KEY): str,
vol.Required(CONF_REFERENCE_ET_1): vol.Coerce(float),
vol.Required(CONF_REFERENCE_ET_2): vol.Coerce(float),
vol.Required(CONF_REFERENCE_ET_3): vol.Coerce(float),
Expand All @@ -101,6 +152,20 @@ async def _show_config_form(self, user_input):
vol.Required(CONF_REFERENCE_ET_10): vol.Coerce(float),
vol.Required(CONF_REFERENCE_ET_11): vol.Coerce(float),
vol.Required(CONF_REFERENCE_ET_12): vol.Coerce(float),
# vol.Required(CONF_NUMBER_OF_SPRINKLERS): vol.Coerce(float),
# vol.Required(CONF_FLOW): vol.Coerce(float),
# vol.Required(CONF_AREA): vol.Coerce(float),
}
),
errors=self._errors,
)

async def _show_step3(self, user_input):
"""Show the configuration form step 2: reference ET values."""
return self.async_show_form(
step_id="step3",
data_schema=vol.Schema(
{
vol.Required(CONF_NUMBER_OF_SPRINKLERS): vol.Coerce(float),
vol.Required(CONF_FLOW): vol.Coerce(float),
vol.Required(CONF_AREA): vol.Coerce(float),
Expand All @@ -109,9 +174,38 @@ async def _show_config_form(self, user_input):
errors=self._errors,
)

async def _show_config_form(self, user_input):
"""Show the configuration form to edit info."""
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_API_KEY): str,
# vol.Required(CONF_REFERENCE_ET_1): vol.Coerce(float),
# vol.Required(CONF_REFERENCE_ET_2): vol.Coerce(float),
# vol.Required(CONF_REFERENCE_ET_3): vol.Coerce(float),
# vol.Required(CONF_REFERENCE_ET_4): vol.Coerce(float),
# vol.Required(CONF_REFERENCE_ET_5): vol.Coerce(float),
# vol.Required(CONF_REFERENCE_ET_6): vol.Coerce(float),
# vol.Required(CONF_REFERENCE_ET_7): vol.Coerce(float),
# vol.Required(CONF_REFERENCE_ET_8): vol.Coerce(float),
# vol.Required(CONF_REFERENCE_ET_9): vol.Coerce(float),
# vol.Required(CONF_REFERENCE_ET_10): vol.Coerce(float),
# vol.Required(CONF_REFERENCE_ET_11): vol.Coerce(float),
# vol.Required(CONF_REFERENCE_ET_12): vol.Coerce(float),
# vol.Required(CONF_NUMBER_OF_SPRINKLERS): vol.Coerce(float),
# vol.Required(CONF_FLOW): vol.Coerce(float),
# vol.Required(CONF_AREA): vol.Coerce(float),
}
),
errors=self._errors,
)

async def _test_api_key(self, api_key):
"""Test access to Open Weather Map API here."""
client = OWMClient(api_key=api_key, latitude=52.353218, longitude=5.0027695)
client = OWMClient(
api_key=api_key.strip(), latitude=52.353218, longitude=5.0027695
)

try:
client.get_data()
Expand Down
2 changes: 1 addition & 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.8"
VERSION = "0.0.9"

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

Expand Down
29 changes: 28 additions & 1 deletion custom_components/smart_irrigation/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"step": {
"user": {
"title": "Smart Irrigation",
"title": "Smart Irrigation - Step 1/3: Open Weather Map API",
"description": "If you need help with the configuration, please see https://github.com/jeroenterheerdt/HASmartIrrigation",
"data": {
"api_key": "API key for Open Weather Map",
Expand All @@ -23,6 +23,33 @@
"flow": "Flow per sprinkler in gpm or lpm",
"area": "Area in sq ft or m2"
}
},
"step2": {
"title": "Smart Irrigation - Step 2/3: Reference evapotranspiration values",
"description": "If you need help with the configuration, please see https://github.com/jeroenterheerdt/HASmartIrrigation",
"data": {
"reference_evapotranspiration_1": "Reference evapotranspiration for January",
"reference_evapotranspiration_2": "Reference evapotranspiration for February",
"reference_evapotranspiration_3": "Reference evapotranspiration for March",
"reference_evapotranspiration_4": "Reference evapotranspiration for April",
"reference_evapotranspiration_5": "Reference evapotranspiration for May",
"reference_evapotranspiration_6": "Reference evapotranspiration for June",
"reference_evapotranspiration_7": "Reference evapotranspiration for July",
"reference_evapotranspiration_8": "Reference evapotranspiration for August",
"reference_evapotranspiration_9": "Reference evapotranspiration for September",
"reference_evapotranspiration_10": "Reference evapotranspiration for October",
"reference_evapotranspiration_11": "Reference evapotranspiration for November",
"reference_evapotranspiration_12": "Reference evapotranspiration for December"
}
},
"step3": {
"title": "Smart Irrigation - Step 3/3: Irrigation system configuration",
"description": "If you need help with the configuration, please see https://github.com/jeroenterheerdt/HASmartIrrigation",
"data": {
"number_of_sprinklers": "Number of sprinklers",
"flow": "Flow per sprinkler in gpm or lpm",
"area": "Area in sq ft or m2"
}
}
},
"error": {
Expand Down
63 changes: 45 additions & 18 deletions custom_components/smart_irrigation/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,54 @@
"title": "Smart Irrigation",
"step": {
"user": {
"title": "Smart Irrigation",
"title": "Smart Irrigation - Step 1/3: Open Weather Map API",
"description": "If you need help with the configuration, please see https://github.com/jeroenterheerdt/HASmartIrrigation",
"data": {
"api_key": "API key for Open Weather Map",
"reference_evapotranspiration_1": "Reference evapotranspiration for January",
"reference_evapotranspiration_2": "Reference evapotranspiration for February",
"reference_evapotranspiration_3": "Reference evapotranspiration for March",
"reference_evapotranspiration_4": "Reference evapotranspiration for April",
"reference_evapotranspiration_5": "Reference evapotranspiration for May",
"reference_evapotranspiration_6": "Reference evapotranspiration for June",
"reference_evapotranspiration_7": "Reference evapotranspiration for July",
"reference_evapotranspiration_8": "Reference evapotranspiration for August",
"reference_evapotranspiration_9": "Reference evapotranspiration for September",
"reference_evapotranspiration_10": "Reference evapotranspiration for October",
"reference_evapotranspiration_11": "Reference evapotranspiration for November",
"reference_evapotranspiration_12": "Reference evapotranspiration for December",
"number_of_sprinklers": "Number of sprinklers",
"flow": "Flow per sprinkler in gpm or lpm",
"area": "Area in sq ft or m2"
"api_key": "API key for Open Weather Map",
"reference_evapotranspiration_1": "Reference evapotranspiration for January",
"reference_evapotranspiration_2": "Reference evapotranspiration for February",
"reference_evapotranspiration_3": "Reference evapotranspiration for March",
"reference_evapotranspiration_4": "Reference evapotranspiration for April",
"reference_evapotranspiration_5": "Reference evapotranspiration for May",
"reference_evapotranspiration_6": "Reference evapotranspiration for June",
"reference_evapotranspiration_7": "Reference evapotranspiration for July",
"reference_evapotranspiration_8": "Reference evapotranspiration for August",
"reference_evapotranspiration_9": "Reference evapotranspiration for September",
"reference_evapotranspiration_10": "Reference evapotranspiration for October",
"reference_evapotranspiration_11": "Reference evapotranspiration for November",
"reference_evapotranspiration_12": "Reference evapotranspiration for December",
"number_of_sprinklers": "Number of sprinklers",
"flow": "Flow per sprinkler in gpm or lpm",
"area": "Area in sq ft or m2"
}
}
},
"step2": {
"title": "Smart Irrigation - Step 2/3: Reference evapotranspiration values",
"description": "If you need help with the configuration, please see https://github.com/jeroenterheerdt/HASmartIrrigation",
"data": {
"reference_evapotranspiration_1": "Reference evapotranspiration for January",
"reference_evapotranspiration_2": "Reference evapotranspiration for February",
"reference_evapotranspiration_3": "Reference evapotranspiration for March",
"reference_evapotranspiration_4": "Reference evapotranspiration for April",
"reference_evapotranspiration_5": "Reference evapotranspiration for May",
"reference_evapotranspiration_6": "Reference evapotranspiration for June",
"reference_evapotranspiration_7": "Reference evapotranspiration for July",
"reference_evapotranspiration_8": "Reference evapotranspiration for August",
"reference_evapotranspiration_9": "Reference evapotranspiration for September",
"reference_evapotranspiration_10": "Reference evapotranspiration for October",
"reference_evapotranspiration_11": "Reference evapotranspiration for November",
"reference_evapotranspiration_12": "Reference evapotranspiration for December"
}
},
"step3": {
"title": "Smart Irrigation - Step 3/3: Irrigation system configuration",
"description": "If you need help with the configuration, please see https://github.com/jeroenterheerdt/HASmartIrrigation",
"data": {
"number_of_sprinklers": "Number of sprinklers",
"flow": "Flow per sprinkler in gpm or lpm",
"area": "Area in sq ft or m2"
}
}
}
},
"title": "Smart Irrigation"
Expand Down

0 comments on commit f212f17

Please sign in to comment.