Skip to content

Commit

Permalink
Accept wrong pin
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknn committed Dec 17, 2024
1 parent f11b580 commit b6509aa
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 9 deletions.
78 changes: 71 additions & 7 deletions custom_components/pax_ble/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .devices.base_device import BaseDevice

from homeassistant.const import CONF_DEVICES
from .const import CONF_ACTION, CONF_ADD_DEVICE, CONF_EDIT_DEVICE, CONF_REMOVE_DEVICE
from .const import CONF_ACTION, CONF_ADD_DEVICE, CONF_WRONG_PIN_SELECTOR, CONF_EDIT_DEVICE, CONF_REMOVE_DEVICE
from .const import DOMAIN, CONF_NAME, CONF_MODEL, CONF_MAC, CONF_PIN, CONF_SCAN_INTERVAL, CONF_SCAN_INTERVAL_FAST
from .const import DEFAULT_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL_FAST
from .const import DeviceModel
Expand All @@ -41,6 +41,7 @@ class PaxConfigFlowHandler(ConfigFlow, domain=DOMAIN):
def __init__(self):
self.device_data = DEVICE_DATA.copy() # Data of "current" device
self.config_entry = None
self.accept_wrong_pin = False

def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
"""Get the options flow for this handler."""
Expand Down Expand Up @@ -107,7 +108,11 @@ async def async_step_add_device(self, user_input=None):

if await fan.connect():
await fan.setAuth(user_input[CONF_PIN])
pin_verified = await fan.checkAuth()

if not self.accept_wrong_pin:
pin_verified = await fan.checkAuth()
else:
pin_verified = True
await fan.disconnect()

if pin_verified:
Expand Down Expand Up @@ -143,9 +148,10 @@ async def async_step_add_device(self, user_input=None):
}
)
else:
errors["base"] = "wrong_pin"
# Store values for new attempt
# Store values for accept / decline wrong pin
self.device_data = user_input
errors["base"] = "wrong_pin"
return await self.async_step_wrong_pin()
else:
errors["base"] = "cannot_connect"
# Store values for new attempt
Expand All @@ -156,10 +162,30 @@ async def async_step_add_device(self, user_input=None):
step_id="add_device", data_schema=data_schema, errors=errors
)

"""##################################################
###################### WRONG PIN ####################
##################################################"""
async def async_step_wrong_pin(self, user_input=None):
"""Accept or decline wrong pin."""
errors = {}

if user_input is not None:
if user_input.get(CONF_WRONG_PIN_SELECTOR) == "accept":
self.accept_wrong_pin = True
return await self.async_step_add_device(self.device_data)
if user_input.get(CONF_WRONG_PIN_SELECTOR) == "decline":
self.accept_wrong_pin = False
return await self.async_step_add_device()

return self.async_show_form(
step_id="wrong_pin", data_schema=MENU_WRONG_PIN_SCHEMA, errors=errors
)

class PaxOptionsFlowHandler(OptionsFlow):
def __init__(self):
self.selected_device = None # Mac address / key
self.device_data = DEVICE_DATA.copy() # Data of "current" device
self.accept_wrong_pin = False

async def async_step_init(self, user_input: dict[str, Any] | None = None):
# Manage the options for the custom component."""
Expand Down Expand Up @@ -198,7 +224,11 @@ async def async_step_add_device(self, user_input=None):

if await fan.connect():
await fan.setAuth(user_input[CONF_PIN])
pin_verified = await fan.checkAuth()

if not self.accept_wrong_pin:
pin_verified = await fan.checkAuth()
else:
pin_verified = True
await fan.disconnect()

if pin_verified:
Expand All @@ -216,9 +246,10 @@ async def async_step_add_device(self, user_input=None):
}
)
else:
errors["base"] = "wrong_pin"
# Store values for new attempt
# Store values for accept / decline wrong pin
self.device_data = user_input
errors["base"] = "wrong_pin"
return await self.async_step_wrong_pin()
else:
errors["base"] = "cannot_connect"
# Store values for new attempt
Expand All @@ -229,6 +260,25 @@ async def async_step_add_device(self, user_input=None):
step_id="add_device", data_schema=data_schema, errors=errors
)

"""##################################################
###################### WRONG PIN ####################
##################################################"""
async def async_step_wrong_pin(self, user_input=None):
"""Accept or decline wrong pin."""
errors = {}

if user_input is not None:
if user_input.get(CONF_WRONG_PIN_SELECTOR) == "accept":
self.accept_wrong_pin = True
return await self.async_step_add_device(self.device_data)
if user_input.get(CONF_WRONG_PIN_SELECTOR) == "decline":
self.accept_wrong_pin = False
return await self.async_step_add_device()

return self.async_show_form(
step_id="wrong_pin", data_schema=MENU_WRONG_PIN_SCHEMA, errors=errors
)

"""##################################################
################# SELECT EDIT DEVICE ################
##################################################"""
Expand Down Expand Up @@ -260,6 +310,7 @@ async def async_step_edit_device(self, user_input=None):
if user_input is not None:
# Update device in config entry
new_data = self.config_entry.data.copy()
new_data[CONF_DEVICES][self.selected_device][CONF_PIN] = user_input[CONF_PIN]
new_data[CONF_DEVICES][self.selected_device][CONF_SCAN_INTERVAL] = user_input[CONF_SCAN_INTERVAL]
new_data[CONF_DEVICES][self.selected_device][CONF_SCAN_INTERVAL_FAST] = user_input[CONF_SCAN_INTERVAL_FAST]

Expand Down Expand Up @@ -384,6 +435,9 @@ def getDeviceSchemaAdd(user_input: dict[str, Any] | None = None) -> vol.Schema:
def getDeviceSchemaEdit(user_input: dict[str, Any] | None = None) -> vol.Schema:
data_schema = vol.Schema(
{
vol.Required(
CONF_PIN, description="Pin Code", default=user_input[CONF_PIN]
): cv.string,
vol.Optional(
CONF_SCAN_INTERVAL, default=user_input[CONF_SCAN_INTERVAL]
): vol.All(vol.Coerce(int), vol.Range(min=5, max=999)),
Expand All @@ -408,3 +462,13 @@ def getDeviceSchemaSelect(devices: dict[str, Any] | None = None) -> vol.Schema:
)

return data_schema

# Schema for accepting wrong pin
MENU_WRONG_PIN_VALUES = ["accept", "decline"]
MENU_WRONG_PIN_SCHEMA = vol.Schema(
{
vol.Required(CONF_WRONG_PIN_SELECTOR): selector.SelectSelector(
selector.SelectSelectorConfig(options=MENU_WRONG_PIN_VALUES, translation_key=CONF_WRONG_PIN_SELECTOR),
)
}
)
1 change: 1 addition & 0 deletions custom_components/pax_ble/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Configuration Constants
CONF_ACTION = "action"
CONF_ADD_DEVICE = "add_device"
CONF_WRONG_PIN_SELECTOR = "wrong_pin_selector"
CONF_EDIT_DEVICE = "edit_device"
CONF_REMOVE_DEVICE = "remove_device"

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions custom_components/pax_ble/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"scan_interval": "Scan Interval in seconds",
"scan_interval_fast": "Fast Scan Interval in seconds"
}
},
"wrong_pin": {
"title": "Pax: Wrong PIN",
"data": {
"wrong_pin_selector": "Select action."
}
}
},
"error": {
Expand Down Expand Up @@ -45,6 +51,12 @@
"scan_interval_fast": "Fast Scan Interval in seconds"
}
},
"wrong_pin": {
"title": "Pax: Wrong PIN",
"data": {
"wrong_pin_selector": "Select action."
}
},
"edit_device": {
"title": "Pax BLE: Edit device",
"description": "Enter new device details for {dev_name}",
Expand Down Expand Up @@ -88,6 +100,12 @@
"edit_device": "Edit device",
"remove_device": "Remove device"
}
},
"wrong_pin_selector": {
"options": {
"accept": "Accept wrong pin",
"decline": "Try new pin"
}
}
},
"services": {
Expand Down
18 changes: 18 additions & 0 deletions custom_components/pax_ble/translations/fi.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"scan_interval": "Päivitysväli sekunneissa",
"scan_interval_fast": "Fast Scan Interval in seconds"
}
},
"wrong_pin": {
"title": "Pax: Wrong PIN",
"data": {
"wrong_pin_selector": "Select action."
}
}
},
"error": {
Expand Down Expand Up @@ -45,6 +51,12 @@
"scan_interval_fast": "Fast Scan Interval in seconds"
}
},
"wrong_pin": {
"title": "Pax: Wrong PIN",
"data": {
"wrong_pin_selector": "Select action."
}
},
"edit_device": {
"title": "Pax BLE: Edit device",
"description": "Enter new device details.",
Expand Down Expand Up @@ -88,6 +100,12 @@
"edit_device": "Edit device",
"remove_device": "Remove device"
}
},
"wrong_pin_selector": {
"options": {
"accept": "Accept wrong pin",
"decline": "Try new pin"
}
}
},
"services": {
Expand Down
18 changes: 18 additions & 0 deletions custom_components/pax_ble/translations/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"scan_interval": "Pollinterval i sekunder",
"scan_interval_fast": "Hurtig Scan Interval i sekunder"
}
},
"wrong_pin": {
"title": "Pax: Feil PIN-kode",
"data": {
"wrong_pin_selector": "Velg aksjon."
}
}
},
"error": {
Expand Down Expand Up @@ -45,6 +51,12 @@
"scan_interval_fast": "Hurtig Scan Interval i sekunder"
}
},
"wrong_pin": {
"title": "Pax: Feil PIN-kode",
"data": {
"wrong_pin_selector": "Velg aksjon."
}
},
"edit_device": {
"title": "Pax BLE: Rediger enhet",
"description": "Angi ny informasjon for {dev_name}",
Expand Down Expand Up @@ -88,6 +100,12 @@
"edit_device": "Rediger enhet",
"remove_device": "Fjern enhet"
}
},
"wrong_pin_selector": {
"options": {
"accept": "Godta feil PIN-kode",
"decline": "Prøv ny PIN-kode"
}
}
},
"services": {
Expand Down
22 changes: 20 additions & 2 deletions custom_components/pax_ble/translations/sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
"scan_interval": "Sökintervall i sekunder",
"scan_interval_fast": "Snabbt skanningsintervall i sekunder"
}
}
},
"wrong_pin": {
"title": "Pax: Wrong PIN",
"data": {
"wrong_pin_selector": "Select action."
}
}
},
"error": {
"cannot_connect": "Anslutning misslyckades",
Expand Down Expand Up @@ -45,6 +51,12 @@
"scan_interval_fast": "Snabbt skanningsintervall i sekunder"
}
},
"wrong_pin": {
"title": "Pax: Wrong PIN",
"data": {
"wrong_pin_selector": "Select action."
}
},
"edit_device": {
"title": "Pax BLE: Redigera enhet",
"description": "Ange nya enhetsdetaljer för {dev_name}",
Expand Down Expand Up @@ -88,6 +100,12 @@
"edit_device": "Redigera enhet",
"remove_device": "Ta bort enhet"
}
},
"wrong_pin_selector": {
"options": {
"accept": "Accept wrong pin",
"decline": "Try new pin"
}
}
},
"services": {
Expand All @@ -102,4 +120,4 @@
}
}
}
}
}

0 comments on commit b6509aa

Please sign in to comment.