Skip to content

Commit

Permalink
Handle cases where openplantbook is not set up
Browse files Browse the repository at this point in the history
  • Loading branch information
Olen committed Aug 14, 2022
1 parent c60e118 commit 8208bbc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
40 changes: 29 additions & 11 deletions custom_components/plant/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,13 @@ async def async_step_select_species(self, user_input=None):
async def async_step_limits(self, user_input=None):
"""Handle max/min values"""

plant_helper = PlantHelper(self.hass)

if user_input is not None:
_LOGGER.debug("User Input %s", user_input)
# Validate user input
valid = await self.validate_step_1(user_input)
if not user_input.get(FLOW_RIGHT_PLANT):
valid = await self.validate_step_3(user_input)
if plant_helper.has_openplantbook and not user_input.get(FLOW_RIGHT_PLANT):
return await self.async_step_select_species()
if valid:
self.plant_info[ATTR_ENTITY_PICTURE] = user_input.get(
Expand All @@ -215,30 +217,31 @@ async def async_step_limits(self, user_input=None):
self.plant_info[OPB_DISPLAY_PID] = user_input.get(OPB_DISPLAY_PID)
user_input.pop(ATTR_ENTITY_PICTURE)
user_input.pop(OPB_DISPLAY_PID)
user_input.pop(FLOW_RIGHT_PLANT)
if FLOW_RIGHT_PLANT in user_input:
user_input.pop(FLOW_RIGHT_PLANT)
self.plant_info[FLOW_PLANT_LIMITS] = user_input
_LOGGER.debug("Plant_info: %s", self.plant_info)
# Return the form of the next step
return await self.async_step_limits_done()

data_schema = {}
plant_helper = PlantHelper(self.hass)
plant_config = await plant_helper.generate_configentry(
config={
ATTR_NAME: self.plant_info[ATTR_NAME],
ATTR_SPECIES: self.plant_info[ATTR_SPECIES],
ATTR_SENSORS: {},
}
)

extra_desc = ""
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 />"
if plant_helper.has_openplantbook:
# 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(
Expand Down Expand Up @@ -373,20 +376,27 @@ async def async_step_limits_done(self, user_input=None):

async def validate_step_1(self, user_input):
"""Validate step one"""
_LOGGER.debug("Validating step 1")
return True

async def validate_step_2(self, user_input):
"""Validate step two"""
_LOGGER.debug("Validating step 2")

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
_LOGGER.debug("Valid")

return True

async def validate_step_3(self, user_input):
"""Validate step three"""
_LOGGER.debug("Validating step 3")

return True

async def validate_step_4(self, user_input):
Expand Down Expand Up @@ -418,15 +428,18 @@ async def async_step_init(

_LOGGER.debug(self.entry.data)
self.plant = self.hass.data[DOMAIN][self.entry.entry_id]["plant"]

plant_helper = PlantHelper(hass=self.hass)
data_schema = {}
data_schema[
vol.Required(
ATTR_SPECIES,
default=self.plant.species,
)
] = str
data_schema[vol.Optional(FLOW_FORCE_SPECIES_UPDATE, default=False)] = cv.boolean
if plant_helper.has_openplantbook:
data_schema[
vol.Optional(FLOW_FORCE_SPECIES_UPDATE, default=False)
] = cv.boolean

display_species = self.plant.display_species or ""
data_schema[
Expand Down Expand Up @@ -534,7 +547,12 @@ async def update_plant_options(
set_entity_id,
value,
)
self.hass.states.async_set(set_entity_id, value)

self.hass.states.async_set(
set_entity_id,
new_state=value,
attributes=self.hass.states.get(set_entity_id).attributes,
)

else:
self.plant.species = new_species
Expand Down
16 changes: 12 additions & 4 deletions custom_components/plant/plant_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,20 @@ class PlantHelper:
def __init__(self, hass: HomeAssistant) -> None:
self.hass = hass

@property
def has_openplantbook(self) -> bool:
"""Helper function to check if openplantbook is available"""
_LOGGER.debug(
"%s in services? %s",
DOMAIN_PLANTBOOK,
DOMAIN_PLANTBOOK in self.hass.services.async_services(),
)
return DOMAIN_PLANTBOOK in self.hass.services.async_services()

async def openplantbook_search(self, species: str) -> dict[str:Any] | None:
"""Search OPB and return list of result"""

if not DOMAIN_PLANTBOOK in self.hass.services.async_services():
_LOGGER.info("%s not in services", DOMAIN_PLANTBOOK)
if not self.has_openplantbook:
return None

try:
Expand Down Expand Up @@ -120,8 +129,7 @@ async def openplantbook_search(self, species: str) -> dict[str:Any] | None:

async def openplantbook_get(self, species: str) -> dict[str:Any] | None:
"""Get information about a plant species from OpenPlantbook"""
if not DOMAIN_PLANTBOOK in self.hass.services.async_services():
_LOGGER.info("%s not in services", DOMAIN_PLANTBOOK)
if not self.has_openplantbook:
return None

plant_get = await self.hass.services.async_call(
Expand Down

0 comments on commit 8208bbc

Please sign in to comment.