From 46fe5d7d161957420fcc3ceed84679d6d31d4129 Mon Sep 17 00:00:00 2001 From: Ola Thoresen Date: Tue, 9 Aug 2022 09:23:09 +0200 Subject: [PATCH] Validate results from Openplantbook Fix image links with local files Various typos etc --- custom_components/plant/config_flow.py | 23 +++++++++++++++++--- custom_components/plant/plant_helpers.py | 14 +++++++++--- custom_components/plant/strings.json | 8 +++---- custom_components/plant/translations/en.json | 8 +++---- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/custom_components/plant/config_flow.py b/custom_components/plant/config_flow.py index 063f3f3..4b7cec9 100644 --- a/custom_components/plant/config_flow.py +++ b/custom_components/plant/config_flow.py @@ -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 ( @@ -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.

" + 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[ @@ -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), @@ -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": "
", + "extra_desc": extra_desc, }, ) @@ -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): diff --git a/custom_components/plant/plant_helpers.py b/custom_components/plant/plant_helpers.py index 9bc8801..80baafe 100644 --- a/custom_components/plant/plant_helpers.py +++ b/custom_components/plant/plant_helpers.py @@ -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: diff --git a/custom_components/plant/strings.json b/custom_components/plant/strings.json index 9c9f9d3..b2ec5c3 100644 --- a/custom_components/plant/strings.json +++ b/custom_components/plant/strings.json @@ -19,7 +19,7 @@ } }, "select_species": { - "title": "Select speices from OpenPlantbook", + "title": "Select species from OpenPlantbook", "description": "{desc}", "data": { "species": "Species" @@ -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", @@ -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", diff --git a/custom_components/plant/translations/en.json b/custom_components/plant/translations/en.json index 9c9f9d3..b2ec5c3 100644 --- a/custom_components/plant/translations/en.json +++ b/custom_components/plant/translations/en.json @@ -19,7 +19,7 @@ } }, "select_species": { - "title": "Select speices from OpenPlantbook", + "title": "Select species from OpenPlantbook", "description": "{desc}", "data": { "species": "Species" @@ -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", @@ -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",