Skip to content

Commit

Permalink
Move consumables codes to separate variable, add new config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeFoodPixels committed Sep 6, 2023
1 parent 3625657 commit 8256940
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 17 deletions.
8 changes: 6 additions & 2 deletions custom_components/robovac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ async def update_device(device):
return

hass_data = entry.data.copy()
if device["gwId"] in hass_data[CONF_VACS] and device.get("ip") is not None:
if (
device["gwId"] in hass_data[CONF_VACS]
and device.get("ip") is not None
and hass_data[CONF_VACS][device["gwId"]].get("autodiscovery", True)
):
if hass_data[CONF_VACS][device["gwId"]][CONF_IP_ADDRESS] != device["ip"]:
hass_data[CONF_VACS][device["gwId"]][CONF_IP_ADDRESS] = device["ip"]
hass.config_entries.async_update_entry(entry, data=hass_data)
Expand Down Expand Up @@ -82,7 +86,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def update_listener(hass, entry):
"""Handle options update."""
hass.config_entries.async_reload(entry.entry_id)
await hass.config_entries.async_reload(entry.entry_id)


def async_get_config_entry_for_device(hass, device_id):
Expand Down
74 changes: 74 additions & 0 deletions custom_components/robovac/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,84 @@ async def async_step_user(
step_id="user", data_schema=USER_SCHEMA, errors=errors
)

@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)


class CannotConnect(HomeAssistantError):
"""Error to indicate we cannot connect."""


class InvalidAuth(HomeAssistantError):
"""Error to indicate there is invalid auth."""


class OptionsFlowHandler(config_entries.OptionsFlow):
"""Handles options flow for the component."""

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
self.config_entry = config_entry
self.selected_vacuum = None

async def async_step_init(self, user_input=None):
errors = {}

if user_input is not None:
self.selected_vacuum = user_input["selected_vacuum"]
return await self.async_step_edit()

vacuums_config = self.config_entry.data[CONF_VACS]
vacuum_list = {}
for id in vacuums_config:
vacuum_list[id] = vacuums_config[id]["name"]

devices_schema = vol.Schema(
{vol.Required("selected_vacuum"): vol.In(vacuum_list)}
)

return self.async_show_form(
step_id="init", data_schema=devices_schema, errors=errors
)

async def async_step_edit(self, user_input=None):
"""Manage the options for the custom component."""
errors = {}

vacuums = self.config_entry.data[CONF_VACS]

if user_input is not None:
updated_vacuums = deepcopy(vacuums)
updated_vacuums[self.selected_vacuum]["autodiscovery"] = user_input[
"autodiscovery"
]
if user_input[CONF_IP_ADDRESS]:
updated_vacuums[self.selected_vacuum][CONF_IP_ADDRESS] = user_input[
CONF_IP_ADDRESS
]

self.hass.config_entries.async_update_entry(
self.config_entry,
data={CONF_VACS: updated_vacuums},
)

return self.async_create_entry(title="", data={})

options_schema = vol.Schema(
{
vol.Required(
"autodiscovery",
default=vacuums[self.selected_vacuum].get("autodiscovery", True),
): bool,
vol.Optional(
CONF_IP_ADDRESS,
default=vacuums[self.selected_vacuum].get(CONF_IP_ADDRESS),
): str,
}
)

return self.async_show_form(
step_id="edit", data_schema=options_schema, errors=errors
)
29 changes: 16 additions & 13 deletions custom_components/robovac/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@
"password": "Password",
"username": "Username"
},
"description": "Enter your Eufy account details"
"description": "Enter your Eufy account details"
}
}
},
"options": {
"error": {
"invalid_path": "The path provided is not valid. Should be in the format `user/repo-name` and should be a valid github repository."
},
"step": {
"init": {
"title": "Manage IPs",
"data": {
"vacuum": "Select the Vacuum to edit",
"ip_address": "IP address of vacuum"
"init": {
"title": "Manage vacuums",
"data": {
"selected_vacuum": "Select the Vacuum to edit"
}
},
"description": "Add or update a vacuums IP address"
}
"edit": {
"title": "Edit vacuum",
"data": {
"autodiscovery": "Enable autodiscovery",
"ip_address": "IP Address"
},
"description": "Autodiscovery will automatically update the IP address"
}
}
}
}
}
}
5 changes: 3 additions & 2 deletions custom_components/robovac/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ class TUYA_CODES(StrEnum):
AUTO_RETURN = "135"
DO_NOT_DISTURB = "107"
BOOST_IQ = "118"
CONSUMABLES = ["142", "116"]

TUYA_CONSUMABLES_CODES = ["142", "116"]


async def async_setup_entry(
Expand Down Expand Up @@ -317,7 +318,7 @@ async def async_update(self):
# self.map_data = self.tuyastatus.get("121")
# self.erro_msg? = self.tuyastatus.get("124")
if self.robovac_supported & RoboVacEntityFeature.CONSUMABLES:
for CONSUMABLE_CODE in TUYA_CODES.CONSUMABLES:
for CONSUMABLE_CODE in TUYA_CONSUMABLES_CODES:
if (
CONSUMABLE_CODE in self.tuyastatus
and self.tuyastatus.get(CONSUMABLE_CODE) is not None
Expand Down

0 comments on commit 8256940

Please sign in to comment.