44from typing import Any
55
66from aiohttp .test_utils import TestClient
7+ import pytest
78
9+ from homeassistant .components import zone
810from homeassistant .const import Platform
911from homeassistant .core import HomeAssistant
12+ from homeassistant .setup import async_setup_component
1013
1114
15+ @pytest .fixture
16+ async def setup_zone (hass : HomeAssistant ) -> None :
17+ """Set up a zone for testing."""
18+ await async_setup_component (
19+ hass ,
20+ zone .DOMAIN ,
21+ {
22+ "zone" : [
23+ {
24+ "name" : "Home" ,
25+ "latitude" : 10.0 ,
26+ "longitude" : 20.0 ,
27+ "radius" : 250 ,
28+ },
29+ {
30+ "name" : "Office" ,
31+ "latitude" : 20.0 ,
32+ "longitude" : 30.0 ,
33+ "radius" : 250 ,
34+ },
35+ {
36+ "name" : "School" ,
37+ "latitude" : 30.0 ,
38+ "longitude" : 40.0 ,
39+ "radius" : 250 ,
40+ },
41+ ]
42+ },
43+ )
44+ await hass .async_block_till_done ()
45+
46+
47+ @pytest .mark .usefixtures ("setup_zone" )
48+ @pytest .mark .parametrize (
49+ ("extra_webhook_data" , "expected_attributes" , "expected_state" ),
50+ [
51+ # Send coordinates + location_name: Location name has precedence
52+ (
53+ {"gps" : [10 , 20 ], "location_name" : "home" },
54+ {"latitude" : 10 , "longitude" : 20 , "gps_accuracy" : 30 },
55+ "home" ,
56+ ),
57+ (
58+ {"gps" : [20 , 30 ], "location_name" : "office" },
59+ {"latitude" : 20 , "longitude" : 30 , "gps_accuracy" : 30 },
60+ "office" ,
61+ ),
62+ (
63+ {"gps" : [30 , 40 ], "location_name" : "school" },
64+ {"latitude" : 30 , "longitude" : 40 , "gps_accuracy" : 30 },
65+ "school" ,
66+ ),
67+ # Send wrong coordinates + location_name: Location name has precedence
68+ (
69+ {"gps" : [10 , 10 ], "location_name" : "home" },
70+ {"latitude" : 10 , "longitude" : 10 , "gps_accuracy" : 30 },
71+ "home" ,
72+ ),
73+ (
74+ {"gps" : [10 , 10 ], "location_name" : "office" },
75+ {"latitude" : 10 , "longitude" : 10 , "gps_accuracy" : 30 },
76+ "office" ,
77+ ),
78+ (
79+ {"gps" : [10 , 10 ], "location_name" : "school" },
80+ {"latitude" : 10 , "longitude" : 10 , "gps_accuracy" : 30 },
81+ "school" ,
82+ ),
83+ # Send location_name only
84+ ({"location_name" : "home" }, {}, "home" ),
85+ ({"location_name" : "office" }, {}, "office" ),
86+ ({"location_name" : "school" }, {}, "school" ),
87+ # Send coordinates only - location is determined by coordinates
88+ (
89+ {"gps" : [10 , 20 ]},
90+ {"latitude" : 10 , "longitude" : 20 , "gps_accuracy" : 30 },
91+ "home" ,
92+ ),
93+ (
94+ {"gps" : [20 , 30 ]},
95+ {"latitude" : 20 , "longitude" : 30 , "gps_accuracy" : 30 },
96+ "Office" ,
97+ ),
98+ (
99+ {"gps" : [30 , 40 ]},
100+ {"latitude" : 30 , "longitude" : 40 , "gps_accuracy" : 30 },
101+ "School" ,
102+ ),
103+ ],
104+ )
12105async def test_sending_location (
13106 hass : HomeAssistant ,
14107 create_registrations : tuple [dict [str , Any ], dict [str , Any ]],
15108 webhook_client : TestClient ,
109+ extra_webhook_data : dict [str , Any ],
110+ expected_attributes : dict [str , Any ],
111+ expected_state : str ,
16112) -> None :
17113 """Test sending a location via a webhook."""
18114 resp = await webhook_client .post (
19115 f"/api/webhook/{ create_registrations [1 ]['webhook_id' ]} " ,
20116 json = {
21117 "type" : "update_location" ,
22118 "data" : {
23- "gps" : [10 , 20 ],
24119 "gps_accuracy" : 30 ,
25120 "battery" : 40 ,
26121 "altitude" : 50 ,
27122 "course" : 60 ,
28123 "speed" : 70 ,
29124 "vertical_accuracy" : 80 ,
30- "location_name" : "bar" ,
31- } ,
125+ }
126+ | extra_webhook_data ,
32127 },
33128 )
34129
@@ -37,16 +132,20 @@ async def test_sending_location(
37132 state = hass .states .get ("device_tracker.test_1_2" )
38133 assert state is not None
39134 assert state .name == "Test 1"
40- assert state .state == "bar"
41- assert state .attributes ["source_type" ] == "gps"
42- assert state .attributes ["latitude" ] == 10
43- assert state .attributes ["longitude" ] == 20
44- assert state .attributes ["gps_accuracy" ] == 30
45- assert state .attributes ["battery_level" ] == 40
46- assert state .attributes ["altitude" ] == 50
47- assert state .attributes ["course" ] == 60
48- assert state .attributes ["speed" ] == 70
49- assert state .attributes ["vertical_accuracy" ] == 80
135+ assert state .state == expected_state
136+ assert (
137+ state .attributes
138+ == {
139+ "friendly_name" : "Test 1" ,
140+ "source_type" : "gps" ,
141+ "battery_level" : 40 ,
142+ "altitude" : 50.0 ,
143+ "course" : 60 ,
144+ "speed" : 70 ,
145+ "vertical_accuracy" : 80 ,
146+ }
147+ | expected_attributes
148+ )
50149
51150 resp = await webhook_client .post (
52151 f"/api/webhook/{ create_registrations [1 ]['webhook_id' ]} " ,
@@ -70,15 +169,18 @@ async def test_sending_location(
70169 state = hass .states .get ("device_tracker.test_1_2" )
71170 assert state is not None
72171 assert state .state == "not_home"
73- assert state .attributes ["source_type" ] == "gps"
74- assert state .attributes ["latitude" ] == 1
75- assert state .attributes ["longitude" ] == 2
76- assert state .attributes ["gps_accuracy" ] == 3
77- assert state .attributes ["battery_level" ] == 4
78- assert state .attributes ["altitude" ] == 5
79- assert state .attributes ["course" ] == 6
80- assert state .attributes ["speed" ] == 7
81- assert state .attributes ["vertical_accuracy" ] == 8
172+ assert state .attributes == {
173+ "friendly_name" : "Test 1" ,
174+ "source_type" : "gps" ,
175+ "latitude" : 1.0 ,
176+ "longitude" : 2.0 ,
177+ "gps_accuracy" : 3 ,
178+ "battery_level" : 4 ,
179+ "altitude" : 5.0 ,
180+ "course" : 6 ,
181+ "speed" : 7 ,
182+ "vertical_accuracy" : 8 ,
183+ }
82184
83185
84186async def test_restoring_location (
0 commit comments