Move consumables codes to separate variable, add new config flow

This commit is contained in:
Luke Bonaccorsi 2023-09-06 10:55:52 +01:00
parent 3625657886
commit 8256940aa0
4 changed files with 99 additions and 17 deletions

View File

@ -41,7 +41,11 @@ async def async_setup(hass, entry) -> bool:
return return
hass_data = entry.data.copy() 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"]: if hass_data[CONF_VACS][device["gwId"]][CONF_IP_ADDRESS] != device["ip"]:
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) hass.config_entries.async_update_entry(entry, data=hass_data)
@ -82,7 +86,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def update_listener(hass, entry): async def update_listener(hass, entry):
"""Handle options update.""" """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): def async_get_config_entry_for_device(hass, device_id):

View File

@ -159,6 +159,12 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
step_id="user", data_schema=USER_SCHEMA, errors=errors 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): class CannotConnect(HomeAssistantError):
"""Error to indicate we cannot connect.""" """Error to indicate we cannot connect."""
@ -166,3 +172,71 @@ class CannotConnect(HomeAssistantError):
class InvalidAuth(HomeAssistantError): class InvalidAuth(HomeAssistantError):
"""Error to indicate there is invalid auth.""" """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
)

View File

@ -15,23 +15,26 @@
"password": "Password", "password": "Password",
"username": "Username" "username": "Username"
}, },
"description": "Enter your Eufy account details" "description": "Enter your Eufy account details"
} }
} }
}, },
"options": { "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": { "step": {
"init": { "init": {
"title": "Manage IPs", "title": "Manage vacuums",
"data": { "data": {
"vacuum": "Select the Vacuum to edit", "selected_vacuum": "Select the Vacuum to edit"
"ip_address": "IP address of vacuum" }
}, },
"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"
}
} }
} }
} }

View File

@ -98,7 +98,8 @@ class TUYA_CODES(StrEnum):
AUTO_RETURN = "135" AUTO_RETURN = "135"
DO_NOT_DISTURB = "107" DO_NOT_DISTURB = "107"
BOOST_IQ = "118" BOOST_IQ = "118"
CONSUMABLES = ["142", "116"]
TUYA_CONSUMABLES_CODES = ["142", "116"]
async def async_setup_entry( async def async_setup_entry(
@ -317,7 +318,7 @@ class RoboVacEntity(StateVacuumEntity):
# self.map_data = self.tuyastatus.get("121") # self.map_data = self.tuyastatus.get("121")
# self.erro_msg? = self.tuyastatus.get("124") # self.erro_msg? = self.tuyastatus.get("124")
if self.robovac_supported & RoboVacEntityFeature.CONSUMABLES: if self.robovac_supported & RoboVacEntityFeature.CONSUMABLES:
for CONSUMABLE_CODE in TUYA_CODES.CONSUMABLES: for CONSUMABLE_CODE in TUYA_CONSUMABLES_CODES:
if ( if (
CONSUMABLE_CODE in self.tuyastatus CONSUMABLE_CODE in self.tuyastatus
and self.tuyastatus.get(CONSUMABLE_CODE) is not None and self.tuyastatus.get(CONSUMABLE_CODE) is not None