Skip to content

Commit d3bf0b3

Browse files
committed
Improve tests and increase coverage
1 parent 993e9dd commit d3bf0b3

File tree

6 files changed

+326
-10
lines changed

6 files changed

+326
-10
lines changed

custom_components/weatherlink/config_flow.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,27 @@ async def validate_input_v2(
113113
if not await hub.authenticate():
114114
raise InvalidAuth
115115

116+
return {}
117+
118+
119+
async def validate_input_v2b(
120+
hass: HomeAssistant, data: dict[str, Any]
121+
) -> dict[str, Any]:
122+
"""Validate the user input allows us to connect.
123+
124+
Data has the keys from STEP_USER_DATA_SCHEMA_V2 with values provided by the user.
125+
"""
126+
websession = async_get_clientsession(hass)
127+
hub = WLHubV2(
128+
station_id=data.get(CONF_STATION_ID),
129+
api_key_v2=data[CONF_API_KEY_V2],
130+
api_secret=data[CONF_API_SECRET],
131+
websession=websession,
132+
)
133+
134+
# if not await hub.authenticate():
135+
# raise InvalidAuth
136+
116137
# Return info that you want to store in the config entry.
117138
data = await hub.get_station()
118139
_LOGGER.debug("Station data: %s", data)
@@ -238,7 +259,7 @@ async def async_step_user_3(
238259
user_input[CONF_API_SECRET] = self.user_data_2[CONF_API_SECRET]
239260

240261
try:
241-
info = await validate_input_v2(self.hass, user_input)
262+
info = await validate_input_v2b(self.hass, user_input)
242263
except CannotConnect:
243264
errors["base"] = "cannot_connect"
244265
except InvalidAuth:

tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ def bypass_get_data_fixture(
7575
yield
7676

7777

78+
@pytest.fixture(name="bypass_get_data_api_1")
79+
def bypass_get_data_api_1_fixture(
80+
hass: HomeAssistant,
81+
):
82+
"""Skip calls to get data from API."""
83+
with patch(
84+
"custom_components.weatherlink.pyweatherlink.WLHub.get_data",
85+
return_value=json_loads(load_fixture("fryksasm_api1.json")),
86+
):
87+
yield
88+
89+
7890
@pytest.fixture(name="bypass_get_station")
7991
def bypass_get_station_fixture(
8092
hass: HomeAssistant,

tests/fixtures/fryksasm_api1.json

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"credit": "Davis Instruments Corp.",
3+
"credit_URL": "http:\/\/www.davisnet.com",
4+
"disclaimer_url": "http:\/\/www.davisnet.com\/about\/terms.asp",
5+
"copyright_url": "http:\/\/www.davisnet.com\/about\/terms.asp",
6+
"privacy_policy_url": "http:\/\/www.davisnet.com\/about\/privacy.asp",
7+
"image": {
8+
"url": "http:\/\/www.weatherlink.com\/images\/Logo_Davis_reflxblu.jpg",
9+
"title": "Davis WeatherLink",
10+
"link": "http:\/\/www.weatherlink.com"
11+
},
12+
"suggested_pickup": "15 minutes after the hour",
13+
"suggested_pickup_period": "60",
14+
"dewpoint_c": "-0.6",
15+
"dewpoint_f": "31.0",
16+
"dewpoint_string": "31.0 F (-0.6 C)",
17+
"heat_index_c": "0.0",
18+
"heat_index_f": "32.0",
19+
"heat_index_string": "32.0 F (0.0 C)",
20+
"location": "Fryks\u00e5s, Dalarna, Sweden",
21+
"latitude": "61.194282356502",
22+
"longitude": "14.5285606384277",
23+
"observation_time": "Last Updated on Dec 29 2024, 11:14 pm CET",
24+
"observation_time_rfc822": "Sun, 29 Dec 2024 23:14:45 +0100",
25+
"pressure_in": "29.409",
26+
"pressure_mb": "995.9",
27+
"pressure_string": "995.9 mb",
28+
"relative_humidity": "97",
29+
"station_id": "petman",
30+
"temp_c": "0.0",
31+
"temp_f": "32.0",
32+
"temperature_string": "32.0 F (0.0 C)",
33+
"wind_degrees": "145",
34+
"wind_dir": "Southeast",
35+
"wind_kt": "5.2",
36+
"wind_mph": "6.0",
37+
"windchill_c": "-2.8",
38+
"windchill_f": "27.0",
39+
"windchill_string": "27.0 F (-2.8 C)",
40+
"davis_current_observation": {
41+
"DID": "001D0A0028EA",
42+
"station_name": "Fryks\u00e5s",
43+
"observation_age": 16,
44+
"dewpoint_day_high_f": "32",
45+
"dewpoint_day_high_time": "12:00am",
46+
"dewpoint_day_low_f": "31",
47+
"dewpoint_day_low_time": "12:00am",
48+
"dewpoint_month_high_f": "43",
49+
"dewpoint_month_low_f": "13",
50+
"dewpoint_year_high_f": "70",
51+
"dewpoint_year_low_f": "-4",
52+
"heat_index_day_high_f": "32",
53+
"heat_index_day_high_time": "12:00am",
54+
"heat_index_month_high_f": "45",
55+
"heat_index_year_high_f": "52",
56+
"pressure_day_high_in": "29.409",
57+
"pressure_day_high_time": "12:00am",
58+
"pressure_day_low_in": "29.406",
59+
"pressure_day_low_time": "12:04am",
60+
"pressure_month_high_in": "30.777",
61+
"pressure_month_low_in": "28.841",
62+
"pressure_tendency_string": "Falling Slowly",
63+
"pressure_year_high_in": "30.777",
64+
"pressure_year_low_in": "28.615",
65+
"rain_day_in": "0.0000",
66+
"rain_month_in": "0.4882",
67+
"rain_rate_day_high_in_per_hr": "0.0000",
68+
"rain_rate_hour_high_in_per_hr": "0.0000",
69+
"rain_rate_in_per_hr": "0.0000",
70+
"rain_rate_month_high_in_per_hr": "0.1260",
71+
"rain_rate_year_high_in_per_hr": "5.9685",
72+
"rain_storm_in": "0.0157",
73+
"rain_storm_start_date": "12\/13\/2024",
74+
"rain_year_in": "28.4094",
75+
"relative_humidity_day_high": "97",
76+
"relative_humidity_day_high_time": "12:00am",
77+
"relative_humidity_day_low": "97",
78+
"relative_humidity_day_low_time": "12:00am",
79+
"relative_humidity_in": "40",
80+
"relative_humidity_in_day_high": "40",
81+
"relative_humidity_in_day_high_time": "12:00am",
82+
"relative_humidity_in_day_low": "40",
83+
"relative_humidity_in_day_low_time": "12:00am",
84+
"relative_humidity_in_month_high": "50",
85+
"relative_humidity_in_month_low": "31",
86+
"relative_humidity_in_year_high": "61",
87+
"relative_humidity_in_year_low": "19",
88+
"sunrise": "10:17am",
89+
"sunset": "3:54pm",
90+
"temp_day_high_f": "32.2",
91+
"temp_day_high_time": "12:00am",
92+
"temp_day_low_f": "32.0",
93+
"temp_day_low_time": "12:14am",
94+
"temp_month_high_f": "44.6",
95+
"temp_month_low_f": "14.1",
96+
"temp_year_high_f": "79.2",
97+
"temp_year_low_f": "-1.0",
98+
"temp_in_day_high_f": "71.4",
99+
"temp_in_day_high_time": "12:00am",
100+
"temp_in_day_low_f": "70.7",
101+
"temp_in_day_low_time": "12:14am",
102+
"temp_in_f": "70.7",
103+
"temp_in_month_high_f": "75.3",
104+
"temp_in_month_low_f": "54.0",
105+
"temp_in_year_high_f": "80.0",
106+
"temp_in_year_low_f": "52.0",
107+
"wind_day_high_mph": "12.0",
108+
"wind_day_high_time": "12:14am",
109+
"wind_month_high_mph": "29.0",
110+
"wind_ten_min_avg_mph": "4.0",
111+
"wind_ten_min_gust_mph": "12.0",
112+
"wind_year_high_mph": "42.0",
113+
"windchill_day_low_f": "27",
114+
"windchill_day_low_time": "12:00am",
115+
"windchill_month_low_f": "10",
116+
"windchill_year_low_f": "3"
117+
},
118+
"time_to_generate": "0.020508 seconds"
119+
}

tests/snapshots/test_binary_sensor.ambr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# serializer version: 1
2-
# name: test_sensor[binary_sensor.strp81_connectivity-entry]
2+
# name: test_binary_sensor[binary_sensor.strp81_connectivity-entry]
33
EntityRegistryEntrySnapshot({
44
'aliases': set({
55
}),
@@ -32,7 +32,7 @@
3232
'unit_of_measurement': None,
3333
})
3434
# ---
35-
# name: test_sensor[binary_sensor.strp81_connectivity-state]
35+
# name: test_binary_sensor[binary_sensor.strp81_connectivity-state]
3636
StateSnapshot({
3737
'attributes': ReadOnlyDict({
3838
'device_class': 'connectivity',
@@ -47,7 +47,7 @@
4747
'state': 'off',
4848
})
4949
# ---
50-
# name: test_sensor[binary_sensor.strp81_transmitter_battery-entry]
50+
# name: test_binary_sensor[binary_sensor.strp81_transmitter_battery-entry]
5151
EntityRegistryEntrySnapshot({
5252
'aliases': set({
5353
}),
@@ -80,7 +80,7 @@
8080
'unit_of_measurement': None,
8181
})
8282
# ---
83-
# name: test_sensor[binary_sensor.strp81_transmitter_battery-state]
83+
# name: test_binary_sensor[binary_sensor.strp81_transmitter_battery-state]
8484
StateSnapshot({
8585
'attributes': ReadOnlyDict({
8686
'device_class': 'battery',

tests/test_binary_sensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
22-
async def test_sensor(
22+
async def test_binary_sensor(
2323
hass: HomeAssistant,
2424
bypass_get_data,
2525
bypass_get_station,

0 commit comments

Comments
 (0)