@@ -108,7 +108,9 @@ async def test_form_zeroconf_link_local_ignored(hass: HomeAssistant) -> None:
108108 assert result ["reason" ] == "link_local_address"
109109
110110
111- async def test_form_zeroconf_ipv4_address (hass : HomeAssistant ) -> None :
111+ async def test_form_zeroconf_ipv4_address (
112+ hass : HomeAssistant , doorbird_api : DoorBird
113+ ) -> None :
112114 """Test we abort and update the ip address from zeroconf with an ipv4 address."""
113115
114116 config_entry = MockConfigEntry (
@@ -118,6 +120,13 @@ async def test_form_zeroconf_ipv4_address(hass: HomeAssistant) -> None:
118120 options = {CONF_EVENTS : ["event1" , "event2" , "event3" ]},
119121 )
120122 config_entry .add_to_hass (hass )
123+
124+ # Mock the API to return the correct MAC when validating
125+ doorbird_api .info .return_value = {
126+ "PRIMARY_MAC_ADDR" : "1CCAE3AAAAAA" ,
127+ "WIFI_MAC_ADDR" : "1CCAE3BBBBBB" ,
128+ }
129+
121130 result = await hass .config_entries .flow .async_init (
122131 DOMAIN ,
123132 context = {"source" : config_entries .SOURCE_ZEROCONF },
@@ -136,6 +145,79 @@ async def test_form_zeroconf_ipv4_address(hass: HomeAssistant) -> None:
136145 assert config_entry .data [CONF_HOST ] == "4.4.4.4"
137146
138147
148+ async def test_form_zeroconf_ipv4_address_wrong_device (
149+ hass : HomeAssistant , doorbird_api : DoorBird
150+ ) -> None :
151+ """Test we abort when the device MAC doesn't match during zeroconf update."""
152+
153+ config_entry = MockConfigEntry (
154+ domain = DOMAIN ,
155+ unique_id = "1CCAE3AAAAAA" ,
156+ data = VALID_CONFIG ,
157+ options = {CONF_EVENTS : ["event1" , "event2" , "event3" ]},
158+ )
159+ config_entry .add_to_hass (hass )
160+
161+ # Mock the API to return a different MAC (wrong device)
162+ doorbird_api .info .return_value = {
163+ "PRIMARY_MAC_ADDR" : "1CCAE3DIFFERENT" , # Different MAC!
164+ "WIFI_MAC_ADDR" : "1CCAE3BBBBBB" ,
165+ }
166+
167+ result = await hass .config_entries .flow .async_init (
168+ DOMAIN ,
169+ context = {"source" : config_entries .SOURCE_ZEROCONF },
170+ data = ZeroconfServiceInfo (
171+ ip_address = ip_address ("4.4.4.4" ),
172+ ip_addresses = [ip_address ("4.4.4.4" )],
173+ hostname = "mock_hostname" ,
174+ name = "Doorstation - abc123._axis-video._tcp.local." ,
175+ port = None ,
176+ properties = {"macaddress" : "1CCAE3AAAAAA" },
177+ type = "mock_type" ,
178+ ),
179+ )
180+ assert result ["type" ] is FlowResultType .ABORT
181+ assert result ["reason" ] == "wrong_device"
182+ # Host should not be updated since it's the wrong device
183+ assert config_entry .data [CONF_HOST ] == "1.2.3.4"
184+
185+
186+ async def test_form_zeroconf_ipv4_address_cannot_connect (
187+ hass : HomeAssistant , doorbird_api : DoorBird
188+ ) -> None :
189+ """Test we abort when we cannot connect to validate during zeroconf update."""
190+
191+ config_entry = MockConfigEntry (
192+ domain = DOMAIN ,
193+ unique_id = "1CCAE3AAAAAA" ,
194+ data = VALID_CONFIG ,
195+ options = {CONF_EVENTS : ["event1" , "event2" , "event3" ]},
196+ )
197+ config_entry .add_to_hass (hass )
198+
199+ # Mock the API to fail connection (e.g., wrong credentials or network error)
200+ doorbird_api .info .side_effect = mock_unauthorized_exception ()
201+
202+ result = await hass .config_entries .flow .async_init (
203+ DOMAIN ,
204+ context = {"source" : config_entries .SOURCE_ZEROCONF },
205+ data = ZeroconfServiceInfo (
206+ ip_address = ip_address ("4.4.4.4" ),
207+ ip_addresses = [ip_address ("4.4.4.4" )],
208+ hostname = "mock_hostname" ,
209+ name = "Doorstation - abc123._axis-video._tcp.local." ,
210+ port = None ,
211+ properties = {"macaddress" : "1CCAE3AAAAAA" },
212+ type = "mock_type" ,
213+ ),
214+ )
215+ assert result ["type" ] is FlowResultType .ABORT
216+ assert result ["reason" ] == "cannot_connect"
217+ # Host should not be updated since we couldn't validate
218+ assert config_entry .data [CONF_HOST ] == "1.2.3.4"
219+
220+
139221async def test_form_zeroconf_non_ipv4_ignored (hass : HomeAssistant ) -> None :
140222 """Test we abort when we get a non ipv4 address via zeroconf."""
141223
0 commit comments