mirror of
https://github.com/home-assistant/core.git
synced 2025-08-13 15:30:03 +00:00
.devcontainer
.github
.vscode
docs
homeassistant
machine
pylint
rootfs
script
tests
auth
backports
components
abode
accuweather
acmeda
adax
adguard
advantage_air
aemet
agent_dvr
air_quality
airly
airnow
airq
airthings
airthings_ble
airtouch4
airvisual
airzone
aladdin_connect
alarm_control_panel
alarmdecoder
alert
alexa
almond
amberelectric
ambiclimate
ambient_station
analytics
android_ip_webcam
androidtv
anthemav
apache_kafka
apcupsd
api
api_streams
apple_tv
application_credentials
apprise
aprs
aranet
arcam_fmj
aseko_pool_live
asuswrt
atag
august
aurora
aurora_abb_powerone
aussie_broadband
auth
automation
awair
aws
axis
azure_devops
azure_event_hub
backup
baf
balboa
bayesian
binary_sensor
blackbird
blebox
blink
bluemaestro
blueprint
bluetooth
bluetooth_le_tracker
bmw_connected_drive
bond
bosch_shc
braviatv
broadlink
brother
brunt
bsblan
bthome
buienradar
button
caldav
calendar
camera
canary
cast
cert_expiry
clicksend_tts
climate
cloud
cloudflare
co2signal
coinbase
color_extractor
comfoconnect
command_line
compensation
config
configurator
control4
conversation
coolmaster
coronavirus
counter
cover
cpuspeed
crownstone
daikin
darksky
datadog
debugpy
deconz
default_config
deluge
demo
denonavr
derivative
device_automation
device_sun_light_trigger
device_tracker
devolo_home_control
devolo_home_network
dexcom
dhcp
diagnostics
dialogflow
directv
discord
discovery
dlna_dmr
dlna_dms
dnsip
doorbird
dsmr
dsmr_reader
dte_energy_bridge
duckdns
dunehd
dynalite
eafm
ecobee
econet
ecowitt
efergy
eight_sleep
elgato
elkm1
elmax
emonitor
emulated_hue
emulated_kasa
emulated_roku
energy
enocean
enphase_envoy
environment_canada
epson
escea
esphome
everlights
evil_genius_labs
ezviz
faa_delays
facebook
facebox
fail2ban
fan
feedreader
ffmpeg
fibaro
fido
file
file_upload
filesize
filter
fireservicerota
firmata
fivem
fjaraskupan
flic
flick_electric
flipr
flo
flume
flux
flux_led
folder
folder_watcher
foobot
forecast_solar
forked_daapd
foscam
freebox
freedns
freedompro
fritz
fritzbox
fritzbox_callmonitor
fronius
frontend
fully_kiosk
garages_amsterdam
gdacs
generic
generic_hygrostat
generic_thermostat
geo_json_events
geo_location
geo_rss_events
geocaching
geofency
geonetnz_quakes
geonetnz_volcano
gios
github
glances
goalzero
gogogate2
goodwe
google
google_assistant
google_domains
google_pubsub
google_sheets
google_translate
google_travel_time
google_wifi
govee_ble
gpslogger
graphite
gree
greeneye_monitor
group
growatt_server
guardian
habitica
hardkernel
hardware
harmony
hassio
hddtemp
hdmi_cec
heos
here_travel_time
hisense_aehw4a1
history
history_stats
hive
hlk_sw16
home_connect
home_plus_control
homeassistant
homeassistant_alerts
homeassistant_hardware
homeassistant_sky_connect
homeassistant_yellow
homekit
homekit_controller
homematic
homematicip_cloud
homewizard
honeywell
html5
http
huawei_lte
hue
huisbaasje
humidifier
hunterdouglas_powerview
hvv_departures
hyperion
ialarm
iaqualink
ibeacon
icloud
ifttt
ign_sismologia
image
image_processing
imap_email_content
influxdb
inkbird
input_boolean
input_button
input_datetime
input_number
input_select
input_text
insteon
integration
intellifire
intent
intent_script
ios
iotawatt
ipma
ipp
iqvia
islamic_prayer_times
iss
isy994
izone
jellyfin
jewish_calendar
juicenet
justnimbus
kaleidescape
keenetic_ndms2
kegtron
keymitt_ble
kira
kmtronic
knx
kodi
konnected
kostal_plenticore
kraken
kulersky
lacrosse_view
lametric
landisgyr_heat_meter
lastfm
launch_library
laundrify
lcn
led_ble
lg_soundbar
lidarr
life360
lifx
light
litejet
litterrobot
livisi
local_file
local_ip
locative
lock
logbook
logentries
logger
logi_circle
london_air
lookin
lovelace
luftdaten
lutron_caseta
lyric
mailbox
mailgun
manual
manual_mqtt
marytts
maxcube
mazda
meater
media_player
media_source
melcloud
melissa
melnor
meraki
met
met_eireann
meteo_france
meteoclimatic
metoffice
mfi
microsoft_face
microsoft_face_detect
microsoft_face_identify
mikrotik
mill
min_max
minecraft_server
minio
mjpeg
moat
mobile_app
mochad
modbus
modem_callerid
modern_forms
moehlenhoff_alpha2
mold_indicator
monoprice
moon
motion_blinds
motioneye
mqtt
mqtt_eventstream
mqtt_json
mqtt_room
mqtt_statestream
mullvad
mutesync
my
myq
mysensors
mythicbeastsdns
nam
namecheapdns
nanoleaf
neato
ness_alarm
nest
netatmo
netgear
network
nexia
nextbus
nextdns
nfandroidtv
nibe_heatpump
nightscout
nina
nmap_tracker
no_ip
nobo_hub
notify
notify_events
notion
nsw_fuel_station
nsw_rural_fire_service_feed
nuheat
nuki
numato
number
nut
nws
nx584
nzbget
octoprint
omnilogic
onboarding
oncue
ondilo_ico
onewire
onvif
open_meteo
openalpr_cloud
openerz
openexchangerates
opengarage
openhardwaremonitor
opentherm_gw
openuv
openweathermap
opnsense
oralb
overkiz
ovo_energy
owntracks
p1_monitor
panasonic_viera
panel_custom
panel_iframe
peco
persistent_notification
person
philips_js
pi_hole
picnic
pilight
ping
plaato
plant
plex
plugwise
plum_lightpad
point
poolsense
powerwall
profiler
progettihwsw
prometheus
prosegur
proximity
prusalink
ps4
pure_energie
push
pushbullet
pushover
pvoutput
pvpc_hourly_pricing
python_script
qingping
qld_bushfire
qnap_qsw
qwikswitch
rachio
radarr
radio_browser
radiotherm
rainforest_eagle
rainmachine
random
raspberry_pi
rdw
recollect_waste
recorder
reddit
remember_the_milk
remote
renault
repairs
rest
rest_command
rflink
rfxtrx
rhasspy
ridwell
ring
risco
rituals_perfume_genie
rmvtransport
roku
roomba
roon
rpi_power
rss_feed_template
rtsp_to_webrtc
ruckus_unleashed
ruuvitag_ble
sabnzbd
safe_mode
samsungtv
scene
schedule
scrape
screenlogic
script
search
season
select
sense
senseme
sensibo
sensirion_ble
sensor
sensorpro
sensorpush
sentry
senz
seventeentrack
sharkiq
shell_command
shelly
shopping_list
sia
sigfox
sighthound
signal_messenger
simplepush
simplisafe
simulated
siren
skybell
slack
sleepiq
slimproto
sma
smappee
smart_meter_texas
smartthings
smarttub
smhi
smtp
snips
snmp
snooz
solaredge
solarlog
solax
soma
somfy_mylink
sonarr
songpal
sonos
soundtouch
spaceapi
spc
speedtestdotnet
spider
spotify
sql
squeezebox
srp_energy
ssdp
starline
startca
statistics
statsd
steam_online
steamist
stookalert
stream
stt
subaru
sun
surepetcare
switch
switch_as_x
switchbee
switchbot
switcher_kis
syncthing
syncthru
synology_dsm
system_bridge
system_health
system_log
tado
tag
tailscale
tankerkoenig
tasmota
tautulli
tcp
telegram
telegram_bot
tellduslive
template
tesla_wall_connector
text
thermobeacon
thermopro
threshold
tibber
tile
tilt_ble
time_date
timer
tod
todoist
tolo
tomato
tomorrowio
toon
totalconnect
tplink
traccar
trace
tractive
tradfri
trafikverket_ferry
trafikverket_train
trafikverket_weatherstation
transmission
transport_nsw
trend
tts
tuya
twentemilieu
twilio
twinkly
twitch
uk_transport
ukraine_alarm
unifi
__init__.py
conftest.py
test_config_flow.py
test_controller.py
test_device_tracker.py
test_diagnostics.py
test_init.py
test_sensor.py
test_services.py
test_switch.py
test_update.py
unifi_direct
unifiprotect
universal
upb
upcloud
update
upnp
uptime
uptimerobot
usb
usgs_earthquakes_feed
utility_meter
uvc
vacuum
vallox
velbus
venstar
vera
verisure
version
vesync
vicare
vilfo
vizio
vlc_telnet
voicerss
volumio
volvooncall
vulcan
vultr
wake_on_lan
wallbox
water_heater
watttime
waze_travel_time
weather
webhook
webostv
websocket_api
wemo
whirlpool
whois
wiffi
wilight
withings
wiz
wled
wolflink
workday
worldclock
ws66i
wsdot
xbox
xiaomi
xiaomi_aqara
xiaomi_ble
xiaomi_miio
yale_smart_alarm
yalexs_ble
yamaha
yamaha_musiccast
yandex_transport
yandextts
yeelight
yolink
youless
zamg
zeroconf
zerproc
zha
zodiac
zone
zwave_js
zwave_me
__init__.py
conftest.py
fixtures
hassfest
helpers
pylint
resources
scripts
test_util
testing_config
util
__init__.py
bandit.yaml
common.py
conftest.py
ignore_uncaught_exceptions.py
test_bootstrap.py
test_config.py
test_config_entries.py
test_core.py
test_data_entry_flow.py
test_exceptions.py
test_loader.py
test_main.py
test_requirements.py
test_runner.py
test_setup.py
test_test_fixtures.py
.core_files.yaml
.coveragerc
.dockerignore
.gitattributes
.gitignore
.hadolint.yaml
.pre-commit-config.yaml
.prettierignore
.readthedocs.yml
.strict-typing
.yamllint
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
Dockerfile.dev
LICENSE.md
MANIFEST.in
README.rst
build.yaml
codecov.yml
mypy.ini
pyproject.toml
requirements.txt
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
requirements_test_pre_commit.txt
setup.cfg
693 lines
22 KiB
Python
693 lines
22 KiB
Python
"""Test UniFi Network config flow."""
|
|
|
|
import socket
|
|
from unittest.mock import patch
|
|
|
|
import aiounifi
|
|
|
|
from homeassistant import config_entries, data_entry_flow
|
|
from homeassistant.components import ssdp
|
|
from homeassistant.components.unifi.config_flow import _async_discover_unifi
|
|
from homeassistant.components.unifi.const import (
|
|
CONF_ALLOW_BANDWIDTH_SENSORS,
|
|
CONF_ALLOW_UPTIME_SENSORS,
|
|
CONF_BLOCK_CLIENT,
|
|
CONF_CONTROLLER,
|
|
CONF_DETECTION_TIME,
|
|
CONF_DPI_RESTRICTIONS,
|
|
CONF_IGNORE_WIRED_BUG,
|
|
CONF_SITE_ID,
|
|
CONF_SSID_FILTER,
|
|
CONF_TRACK_CLIENTS,
|
|
CONF_TRACK_DEVICES,
|
|
CONF_TRACK_WIRED_CLIENTS,
|
|
DOMAIN as UNIFI_DOMAIN,
|
|
)
|
|
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
|
from homeassistant.const import (
|
|
CONF_HOST,
|
|
CONF_PASSWORD,
|
|
CONF_PORT,
|
|
CONF_USERNAME,
|
|
CONF_VERIFY_SSL,
|
|
CONTENT_TYPE_JSON,
|
|
)
|
|
|
|
from .test_controller import setup_unifi_integration
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
CLIENTS = [{"mac": "00:00:00:00:00:01"}]
|
|
|
|
DEVICES = [
|
|
{
|
|
"board_rev": 21,
|
|
"device_id": "mock-id",
|
|
"ip": "10.0.1.1",
|
|
"last_seen": 0,
|
|
"mac": "00:00:00:00:01:01",
|
|
"model": "U7PG2",
|
|
"name": "access_point",
|
|
"state": 1,
|
|
"type": "uap",
|
|
"version": "4.0.80.10875",
|
|
"wlan_overrides": [
|
|
{
|
|
"name": "SSID 3",
|
|
"radio": "na",
|
|
"radio_name": "wifi1",
|
|
"wlan_id": "012345678910111213141516",
|
|
},
|
|
{
|
|
"name": "",
|
|
"radio": "na",
|
|
"radio_name": "wifi1",
|
|
"wlan_id": "012345678910111213141516",
|
|
},
|
|
{
|
|
"radio": "na",
|
|
"radio_name": "wifi1",
|
|
"wlan_id": "012345678910111213141516",
|
|
},
|
|
],
|
|
}
|
|
]
|
|
|
|
WLANS = [
|
|
{"name": "SSID 1"},
|
|
{"name": "SSID 2", "name_combine_enabled": False, "name_combine_suffix": "_IOT"},
|
|
]
|
|
|
|
DPI_GROUPS = [
|
|
{
|
|
"_id": "5ba29dd8e3c58f026e9d7c4a",
|
|
"name": "Default",
|
|
"site_id": "5ba29dd4e3c58f026e9d7c38",
|
|
},
|
|
]
|
|
|
|
|
|
async def test_flow_works(hass, aioclient_mock, mock_discovery):
|
|
"""Test config flow."""
|
|
mock_discovery.return_value = "1"
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["data_schema"]({CONF_USERNAME: "", CONF_PASSWORD: ""}) == {
|
|
CONF_HOST: "unifi",
|
|
CONF_USERNAME: "",
|
|
CONF_PASSWORD: "",
|
|
CONF_PORT: 443,
|
|
CONF_VERIFY_SSL: False,
|
|
}
|
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
aioclient_mock.post(
|
|
"https://1.2.3.4:1234/api/login",
|
|
json={"data": "login successful", "meta": {"rc": "ok"}},
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
"https://1.2.3.4:1234/api/self/sites",
|
|
json={
|
|
"data": [
|
|
{"desc": "Site name", "name": "site_id", "role": "admin", "_id": "1"}
|
|
],
|
|
"meta": {"rc": "ok"},
|
|
},
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
)
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input={
|
|
CONF_HOST: "1.2.3.4",
|
|
CONF_USERNAME: "username",
|
|
CONF_PASSWORD: "password",
|
|
CONF_PORT: 1234,
|
|
CONF_VERIFY_SSL: True,
|
|
},
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "Site name"
|
|
assert result["data"] == {
|
|
CONF_HOST: "1.2.3.4",
|
|
CONF_USERNAME: "username",
|
|
CONF_PASSWORD: "password",
|
|
CONF_PORT: 1234,
|
|
CONF_SITE_ID: "site_id",
|
|
CONF_VERIFY_SSL: True,
|
|
CONF_CONTROLLER: {
|
|
CONF_HOST: "1.2.3.4",
|
|
CONF_USERNAME: "username",
|
|
CONF_PASSWORD: "password",
|
|
CONF_PORT: 1234,
|
|
CONF_SITE_ID: "site_id",
|
|
CONF_VERIFY_SSL: True,
|
|
},
|
|
}
|
|
|
|
|
|
async def test_flow_works_negative_discovery(hass, aioclient_mock, mock_discovery):
|
|
"""Test config flow with a negative outcome of async_discovery_unifi."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["data_schema"]({CONF_USERNAME: "", CONF_PASSWORD: ""}) == {
|
|
CONF_HOST: "",
|
|
CONF_USERNAME: "",
|
|
CONF_PASSWORD: "",
|
|
CONF_PORT: 443,
|
|
CONF_VERIFY_SSL: False,
|
|
}
|
|
|
|
|
|
async def test_flow_multiple_sites(hass, aioclient_mock):
|
|
"""Test config flow works when finding multiple sites."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
aioclient_mock.post(
|
|
"https://1.2.3.4:1234/api/login",
|
|
json={"data": "login successful", "meta": {"rc": "ok"}},
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
"https://1.2.3.4:1234/api/self/sites",
|
|
json={
|
|
"data": [
|
|
{"name": "default", "role": "admin", "desc": "site name", "_id": "1"},
|
|
{"name": "site2", "role": "admin", "desc": "site2 name", "_id": "2"},
|
|
],
|
|
"meta": {"rc": "ok"},
|
|
},
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
)
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input={
|
|
CONF_HOST: "1.2.3.4",
|
|
CONF_USERNAME: "username",
|
|
CONF_PASSWORD: "password",
|
|
CONF_PORT: 1234,
|
|
CONF_VERIFY_SSL: True,
|
|
},
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "site"
|
|
assert result["data_schema"]({"site": "1"})
|
|
assert result["data_schema"]({"site": "2"})
|
|
|
|
|
|
async def test_flow_raise_already_configured(hass, aioclient_mock):
|
|
"""Test config flow aborts since a connected config entry already exists."""
|
|
await setup_unifi_integration(hass, aioclient_mock)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
|
|
aioclient_mock.clear_requests()
|
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
aioclient_mock.post(
|
|
"https://1.2.3.4:1234/api/login",
|
|
json={"data": "login successful", "meta": {"rc": "ok"}},
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
"https://1.2.3.4:1234/api/self/sites",
|
|
json={
|
|
"data": [
|
|
{"desc": "Site name", "name": "site_id", "role": "admin", "_id": "1"}
|
|
],
|
|
"meta": {"rc": "ok"},
|
|
},
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
)
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input={
|
|
CONF_HOST: "1.2.3.4",
|
|
CONF_USERNAME: "username",
|
|
CONF_PASSWORD: "password",
|
|
CONF_PORT: 1234,
|
|
CONF_VERIFY_SSL: True,
|
|
},
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
async def test_flow_aborts_configuration_updated(hass, aioclient_mock):
|
|
"""Test config flow aborts since a connected config entry already exists."""
|
|
entry = MockConfigEntry(
|
|
domain=UNIFI_DOMAIN, data={"host": "1.2.3.4", "site": "office"}, unique_id="2"
|
|
)
|
|
entry.add_to_hass(hass)
|
|
|
|
entry = MockConfigEntry(
|
|
domain=UNIFI_DOMAIN, data={"host": "1.2.3.4", "site": "site_id"}, unique_id="1"
|
|
)
|
|
entry.add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
aioclient_mock.post(
|
|
"https://1.2.3.4:1234/api/login",
|
|
json={"data": "login successful", "meta": {"rc": "ok"}},
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
"https://1.2.3.4:1234/api/self/sites",
|
|
json={
|
|
"data": [
|
|
{"desc": "Site name", "name": "site_id", "role": "admin", "_id": "1"}
|
|
],
|
|
"meta": {"rc": "ok"},
|
|
},
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
)
|
|
|
|
with patch("homeassistant.components.unifi.async_setup_entry"):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input={
|
|
CONF_HOST: "1.2.3.4",
|
|
CONF_USERNAME: "username",
|
|
CONF_PASSWORD: "password",
|
|
CONF_PORT: 1234,
|
|
CONF_VERIFY_SSL: True,
|
|
},
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
|
assert result["reason"] == "configuration_updated"
|
|
|
|
|
|
async def test_flow_fails_user_credentials_faulty(hass, aioclient_mock):
|
|
"""Test config flow."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
with patch("aiounifi.Controller.login", side_effect=aiounifi.errors.Unauthorized):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input={
|
|
CONF_HOST: "1.2.3.4",
|
|
CONF_USERNAME: "username",
|
|
CONF_PASSWORD: "password",
|
|
CONF_PORT: 1234,
|
|
CONF_VERIFY_SSL: True,
|
|
},
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["errors"] == {"base": "faulty_credentials"}
|
|
|
|
|
|
async def test_flow_fails_controller_unavailable(hass, aioclient_mock):
|
|
"""Test config flow."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
with patch("aiounifi.Controller.login", side_effect=aiounifi.errors.RequestError):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input={
|
|
CONF_HOST: "1.2.3.4",
|
|
CONF_USERNAME: "username",
|
|
CONF_PASSWORD: "password",
|
|
CONF_PORT: 1234,
|
|
CONF_VERIFY_SSL: True,
|
|
},
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["errors"] == {"base": "service_unavailable"}
|
|
|
|
|
|
async def test_reauth_flow_update_configuration(hass, aioclient_mock):
|
|
"""Verify reauth flow can update controller configuration."""
|
|
config_entry = await setup_unifi_integration(hass, aioclient_mock)
|
|
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
|
controller.available = False
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN,
|
|
context={
|
|
"source": SOURCE_REAUTH,
|
|
"unique_id": config_entry.unique_id,
|
|
"entry_id": config_entry.entry_id,
|
|
},
|
|
data=config_entry.data,
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == SOURCE_USER
|
|
|
|
aioclient_mock.clear_requests()
|
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
aioclient_mock.post(
|
|
"https://1.2.3.4:1234/api/login",
|
|
json={"data": "login successful", "meta": {"rc": "ok"}},
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
"https://1.2.3.4:1234/api/self/sites",
|
|
json={
|
|
"data": [
|
|
{"desc": "Site name", "name": "site_id", "role": "admin", "_id": "1"}
|
|
],
|
|
"meta": {"rc": "ok"},
|
|
},
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
)
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input={
|
|
CONF_HOST: "1.2.3.4",
|
|
CONF_USERNAME: "new_name",
|
|
CONF_PASSWORD: "new_pass",
|
|
CONF_PORT: 1234,
|
|
CONF_VERIFY_SSL: True,
|
|
},
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
|
assert result["reason"] == "reauth_successful"
|
|
assert config_entry.data[CONF_HOST] == "1.2.3.4"
|
|
assert config_entry.data[CONF_USERNAME] == "new_name"
|
|
assert config_entry.data[CONF_PASSWORD] == "new_pass"
|
|
|
|
|
|
async def test_advanced_option_flow(hass, aioclient_mock):
|
|
"""Test advanced config flow options."""
|
|
config_entry = await setup_unifi_integration(
|
|
hass,
|
|
aioclient_mock,
|
|
clients_response=CLIENTS,
|
|
devices_response=DEVICES,
|
|
wlans_response=WLANS,
|
|
dpigroup_response=DPI_GROUPS,
|
|
dpiapp_response=[],
|
|
)
|
|
|
|
result = await hass.config_entries.options.async_init(
|
|
config_entry.entry_id, context={"show_advanced_options": True}
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "device_tracker"
|
|
assert not result["last_step"]
|
|
assert set(
|
|
result["data_schema"].schema[CONF_SSID_FILTER].options.keys()
|
|
).intersection(("SSID 1", "SSID 2", "SSID 2_IOT", "SSID 3"))
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
result["flow_id"],
|
|
user_input={
|
|
CONF_TRACK_CLIENTS: False,
|
|
CONF_TRACK_WIRED_CLIENTS: False,
|
|
CONF_TRACK_DEVICES: False,
|
|
CONF_SSID_FILTER: ["SSID 1", "SSID 2_IOT", "SSID 3"],
|
|
CONF_DETECTION_TIME: 100,
|
|
},
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "client_control"
|
|
assert not result["last_step"]
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
result["flow_id"],
|
|
user_input={
|
|
CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]],
|
|
CONF_DPI_RESTRICTIONS: False,
|
|
},
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "statistics_sensors"
|
|
assert result["last_step"]
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
result["flow_id"],
|
|
user_input={
|
|
CONF_ALLOW_BANDWIDTH_SENSORS: True,
|
|
CONF_ALLOW_UPTIME_SENSORS: True,
|
|
},
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
|
assert result["data"] == {
|
|
CONF_TRACK_CLIENTS: False,
|
|
CONF_TRACK_WIRED_CLIENTS: False,
|
|
CONF_TRACK_DEVICES: False,
|
|
CONF_SSID_FILTER: ["SSID 1", "SSID 2_IOT", "SSID 3"],
|
|
CONF_DETECTION_TIME: 100,
|
|
CONF_IGNORE_WIRED_BUG: False,
|
|
CONF_DPI_RESTRICTIONS: False,
|
|
CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]],
|
|
CONF_ALLOW_BANDWIDTH_SENSORS: True,
|
|
CONF_ALLOW_UPTIME_SENSORS: True,
|
|
}
|
|
|
|
|
|
async def test_simple_option_flow(hass, aioclient_mock):
|
|
"""Test simple config flow options."""
|
|
config_entry = await setup_unifi_integration(
|
|
hass,
|
|
aioclient_mock,
|
|
clients_response=CLIENTS,
|
|
wlans_response=WLANS,
|
|
dpigroup_response=DPI_GROUPS,
|
|
dpiapp_response=[],
|
|
)
|
|
|
|
result = await hass.config_entries.options.async_init(
|
|
config_entry.entry_id, context={"show_advanced_options": False}
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
assert result["step_id"] == "simple_options"
|
|
assert result["last_step"]
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
result["flow_id"],
|
|
user_input={
|
|
CONF_TRACK_CLIENTS: False,
|
|
CONF_TRACK_DEVICES: False,
|
|
CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]],
|
|
},
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
|
assert result["data"] == {
|
|
CONF_TRACK_CLIENTS: False,
|
|
CONF_TRACK_DEVICES: False,
|
|
CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]],
|
|
}
|
|
|
|
|
|
async def test_option_flow_integration_not_setup(hass, aioclient_mock):
|
|
"""Test advanced config flow options."""
|
|
config_entry = await setup_unifi_integration(hass, aioclient_mock)
|
|
|
|
hass.data[UNIFI_DOMAIN].pop(config_entry.entry_id)
|
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
|
|
|
assert result["type"] == "abort"
|
|
assert result["reason"] == "integration_not_setup"
|
|
|
|
|
|
async def test_form_ssdp(hass):
|
|
"""Test we get the form with ssdp source."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN,
|
|
context={"source": config_entries.SOURCE_SSDP},
|
|
data=ssdp.SsdpServiceInfo(
|
|
ssdp_usn="mock_usn",
|
|
ssdp_st="mock_st",
|
|
ssdp_location="http://192.168.208.1:41417/rootDesc.xml",
|
|
upnp={
|
|
"friendlyName": "UniFi Dream Machine",
|
|
"modelDescription": "UniFi Dream Machine Pro",
|
|
"serialNumber": "e0:63:da:20:14:a9",
|
|
},
|
|
),
|
|
)
|
|
assert result["type"] == "form"
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {}
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
assert len(flows) == 1
|
|
|
|
assert (
|
|
flows[0].get("context", {}).get("configuration_url")
|
|
== "https://192.168.208.1:443"
|
|
)
|
|
|
|
context = next(
|
|
flow["context"]
|
|
for flow in hass.config_entries.flow.async_progress()
|
|
if flow["flow_id"] == result["flow_id"]
|
|
)
|
|
assert context["title_placeholders"] == {
|
|
"host": "192.168.208.1",
|
|
"site": "default",
|
|
}
|
|
|
|
|
|
async def test_form_ssdp_aborts_if_host_already_exists(hass):
|
|
"""Test we abort if the host is already configured."""
|
|
|
|
entry = MockConfigEntry(
|
|
domain=UNIFI_DOMAIN,
|
|
data={"host": "192.168.208.1", "site": "site_id"},
|
|
)
|
|
entry.add_to_hass(hass)
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN,
|
|
context={"source": config_entries.SOURCE_SSDP},
|
|
data=ssdp.SsdpServiceInfo(
|
|
ssdp_usn="mock_usn",
|
|
ssdp_st="mock_st",
|
|
ssdp_location="http://192.168.208.1:41417/rootDesc.xml",
|
|
upnp={
|
|
"friendlyName": "UniFi Dream Machine",
|
|
"modelDescription": "UniFi Dream Machine Pro",
|
|
"serialNumber": "e0:63:da:20:14:a9",
|
|
},
|
|
),
|
|
)
|
|
assert result["type"] == "abort"
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
async def test_form_ssdp_aborts_if_serial_already_exists(hass):
|
|
"""Test we abort if the serial is already configured."""
|
|
|
|
entry = MockConfigEntry(
|
|
domain=UNIFI_DOMAIN,
|
|
data={"controller": {"host": "1.2.3.4", "site": "site_id"}},
|
|
unique_id="e0:63:da:20:14:a9",
|
|
)
|
|
entry.add_to_hass(hass)
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN,
|
|
context={"source": config_entries.SOURCE_SSDP},
|
|
data=ssdp.SsdpServiceInfo(
|
|
ssdp_usn="mock_usn",
|
|
ssdp_st="mock_st",
|
|
ssdp_location="http://192.168.208.1:41417/rootDesc.xml",
|
|
upnp={
|
|
"friendlyName": "UniFi Dream Machine",
|
|
"modelDescription": "UniFi Dream Machine Pro",
|
|
"serialNumber": "e0:63:da:20:14:a9",
|
|
},
|
|
),
|
|
)
|
|
assert result["type"] == "abort"
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
async def test_form_ssdp_gets_form_with_ignored_entry(hass):
|
|
"""Test we can still setup if there is an ignored entry."""
|
|
|
|
entry = MockConfigEntry(
|
|
domain=UNIFI_DOMAIN,
|
|
data={"not_controller_key": None},
|
|
source=config_entries.SOURCE_IGNORE,
|
|
)
|
|
entry.add_to_hass(hass)
|
|
result = await hass.config_entries.flow.async_init(
|
|
UNIFI_DOMAIN,
|
|
context={"source": config_entries.SOURCE_SSDP},
|
|
data=ssdp.SsdpServiceInfo(
|
|
ssdp_usn="mock_usn",
|
|
ssdp_st="mock_st",
|
|
ssdp_location="http://1.2.3.4:41417/rootDesc.xml",
|
|
upnp={
|
|
"friendlyName": "UniFi Dream Machine New",
|
|
"modelDescription": "UniFi Dream Machine Pro",
|
|
"serialNumber": "e0:63:da:20:14:a9",
|
|
},
|
|
),
|
|
)
|
|
assert result["type"] == "form"
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {}
|
|
context = next(
|
|
flow["context"]
|
|
for flow in hass.config_entries.flow.async_progress()
|
|
if flow["flow_id"] == result["flow_id"]
|
|
)
|
|
assert context["title_placeholders"] == {
|
|
"host": "1.2.3.4",
|
|
"site": "default",
|
|
}
|
|
|
|
|
|
async def test_discover_unifi_positive(hass):
|
|
"""Verify positive run of UniFi discovery."""
|
|
with patch("socket.gethostbyname", return_value=True):
|
|
assert await _async_discover_unifi(hass)
|
|
|
|
|
|
async def test_discover_unifi_negative(hass):
|
|
"""Verify negative run of UniFi discovery."""
|
|
with patch("socket.gethostbyname", side_effect=socket.gaierror):
|
|
assert await _async_discover_unifi(hass) is None
|