Skip to content

Commit

Permalink
Validate results from Openplantbook
Browse files Browse the repository at this point in the history
Fix image links with local files
Various typos etc
  • Loading branch information
Olen committed Aug 9, 2022
1 parent e32db24 commit 46fe5d7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
23 changes: 20 additions & 3 deletions custom_components/plant/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.network import get_url
from homeassistant.helpers.selector import selector

from .const import (
Expand Down Expand Up @@ -232,11 +233,16 @@ async def async_step_limits(self, user_input=None):
if plant_config[FLOW_PLANT_INFO].get(OPB_DISPLAY_PID):
# We got data from OPB. Display a "wrong plant" switch
data_schema[vol.Optional(FLOW_RIGHT_PLANT, default=True)] = cv.boolean

extra_desc = ""
display_pid = plant_config[FLOW_PLANT_INFO].get(OPB_DISPLAY_PID)
else:
# We did not get any data from OPB. Show a warning
extra_desc = f"Did not find **«{self.plant_info[ATTR_SEARCH_FOR]}»** in OpenPlantbook. Using default values for thresholds.<br /><br />"
display_pid = self.plant_info[ATTR_SEARCH_FOR]
data_schema[
vol.Required(
OPB_DISPLAY_PID,
default=plant_config[FLOW_PLANT_INFO].get(OPB_DISPLAY_PID, ""),
default=display_pid,
)
] = str
data_schema[
Expand Down Expand Up @@ -338,7 +344,11 @@ async def async_step_limits(self, user_input=None):
default=plant_config[FLOW_PLANT_INFO].get(ATTR_ENTITY_PICTURE),
)
] = str

entity_picture = plant_config[FLOW_PLANT_INFO].get(ATTR_ENTITY_PICTURE)
if not entity_picture.startswith("http"):
entity_picture = (
f"{get_url(self.hass, require_current_request=True)}{entity_picture}"
)
return self.async_show_form(
step_id="limits",
data_schema=vol.Schema(data_schema),
Expand All @@ -349,6 +359,7 @@ async def async_step_limits(self, user_input=None):
ATTR_NAME: plant_config[FLOW_PLANT_INFO].get(ATTR_NAME),
FLOW_TEMP_UNIT: self.hass.config.units.temperature_unit,
"br": "<br />",
"extra_desc": extra_desc,
},
)

Expand All @@ -365,6 +376,12 @@ async def validate_step_1(self, user_input):

async def validate_step_2(self, user_input):
"""Validate step two"""
if not ATTR_SPECIES in user_input:
return False
if not isinstance(user_input[ATTR_SPECIES], str):
return False
if len(user_input[ATTR_SPECIES]) < 5:
return False
return True

async def validate_step_3(self, user_input):
Expand Down
14 changes: 11 additions & 3 deletions custom_components/plant/plant_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,17 @@ async def openplantbook_search(self, species: str) -> dict[str:Any] | None:
"Result: %s",
self.hass.states.get(f"{DOMAIN_PLANTBOOK}.{OPB_SEARCH_RESULT}"),
)
return self.hass.states.get(
f"{DOMAIN_PLANTBOOK}.{OPB_SEARCH_RESULT}"
).attributes
if (
int(
self.hass.states.get(
f"{DOMAIN_PLANTBOOK}.{OPB_SEARCH_RESULT}"
).state
)
> 0
):
return self.hass.states.get(
f"{DOMAIN_PLANTBOOK}.{OPB_SEARCH_RESULT}"
).attributes
return None

async def openplantbook_get(self, species: str) -> dict[str:Any] | None:
Expand Down
8 changes: 4 additions & 4 deletions custom_components/plant/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
},
"select_species": {
"title": "Select speices from OpenPlantbook",
"title": "Select species from OpenPlantbook",
"description": "{desc}",
"data": {
"species": "Species"
Expand All @@ -30,7 +30,7 @@
},
"limits": {
"title": "Set limits",
"description": "# {name}\n![Plant]({entity_picture}){br}\nTemperature unit is {temperature_unit}",
"description": "# {name}\n![Plant]({entity_picture}){br}\n{extra_desc}Temperature unit is {temperature_unit}",
"data": {
"right_plant": "This is the plant I was looking for",
"display_pid": "Plant species to display",
Expand Down Expand Up @@ -65,8 +65,8 @@
"conductivity_sensor": "Conductivity sensor",
"illuminance_sensor": "Illuminance sensor",
"humidity_sensor": "Air humidity sensor",
"entity_picture": "Image URL. Will be automatically updated if species is found in OpnePlantbook",
"illuminance_trigger": "Use illuminance and as problem trigger",
"entity_picture": "Image URL. Will be automatically updated if species is found in OpenPlantbook",
"illuminance_trigger": "Use illuminance as problem trigger",
"dli_trigger": "Use daily light integral (dli) and as problem trigger",
"humidity_trigger": "Use air humidity as problem trigger",
"conductivity_trigger": "Use conductivity as problem trigger",
Expand Down
8 changes: 4 additions & 4 deletions custom_components/plant/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
},
"select_species": {
"title": "Select speices from OpenPlantbook",
"title": "Select species from OpenPlantbook",
"description": "{desc}",
"data": {
"species": "Species"
Expand All @@ -30,7 +30,7 @@
},
"limits": {
"title": "Set limits",
"description": "# {name}\n![Plant]({entity_picture}){br}\nTemperature unit is {temperature_unit}",
"description": "# {name}\n![Plant]({entity_picture}){br}\n{extra_desc}Temperature unit is {temperature_unit}",
"data": {
"right_plant": "This is the plant I was looking for",
"display_pid": "Plant species to display",
Expand Down Expand Up @@ -65,8 +65,8 @@
"conductivity_sensor": "Conductivity sensor",
"illuminance_sensor": "Illuminance sensor",
"humidity_sensor": "Air humidity sensor",
"entity_picture": "Image URL. Will be automatically updated if species is found in OpnePlantbook",
"illuminance_trigger": "Use illuminance and as problem trigger",
"entity_picture": "Image URL. Will be automatically updated if species is found in OpenPlantbook",
"illuminance_trigger": "Use illuminance as problem trigger",
"dli_trigger": "Use daily light integral (dli) and as problem trigger",
"humidity_trigger": "Use air humidity as problem trigger",
"conductivity_trigger": "Use conductivity as problem trigger",
Expand Down

0 comments on commit 46fe5d7

Please sign in to comment.