Skip to content

Commit

Permalink
Improve tests and increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
astrandb committed Dec 31, 2024
1 parent 993e9dd commit d3bf0b3
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 10 deletions.
23 changes: 22 additions & 1 deletion custom_components/weatherlink/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ async def validate_input_v2(
if not await hub.authenticate():
raise InvalidAuth

return {}


async def validate_input_v2b(
hass: HomeAssistant, data: dict[str, Any]
) -> dict[str, Any]:
"""Validate the user input allows us to connect.
Data has the keys from STEP_USER_DATA_SCHEMA_V2 with values provided by the user.
"""
websession = async_get_clientsession(hass)
hub = WLHubV2(
station_id=data.get(CONF_STATION_ID),
api_key_v2=data[CONF_API_KEY_V2],
api_secret=data[CONF_API_SECRET],
websession=websession,
)

# if not await hub.authenticate():
# raise InvalidAuth

# Return info that you want to store in the config entry.
data = await hub.get_station()
_LOGGER.debug("Station data: %s", data)
Expand Down Expand Up @@ -238,7 +259,7 @@ async def async_step_user_3(
user_input[CONF_API_SECRET] = self.user_data_2[CONF_API_SECRET]

try:
info = await validate_input_v2(self.hass, user_input)
info = await validate_input_v2b(self.hass, user_input)
except CannotConnect:
errors["base"] = "cannot_connect"
except InvalidAuth:
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ def bypass_get_data_fixture(
yield


@pytest.fixture(name="bypass_get_data_api_1")
def bypass_get_data_api_1_fixture(
hass: HomeAssistant,
):
"""Skip calls to get data from API."""
with patch(
"custom_components.weatherlink.pyweatherlink.WLHub.get_data",
return_value=json_loads(load_fixture("fryksasm_api1.json")),
):
yield


@pytest.fixture(name="bypass_get_station")
def bypass_get_station_fixture(
hass: HomeAssistant,
Expand Down
119 changes: 119 additions & 0 deletions tests/fixtures/fryksasm_api1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"credit": "Davis Instruments Corp.",
"credit_URL": "http:\/\/www.davisnet.com",
"disclaimer_url": "http:\/\/www.davisnet.com\/about\/terms.asp",
"copyright_url": "http:\/\/www.davisnet.com\/about\/terms.asp",
"privacy_policy_url": "http:\/\/www.davisnet.com\/about\/privacy.asp",
"image": {
"url": "http:\/\/www.weatherlink.com\/images\/Logo_Davis_reflxblu.jpg",
"title": "Davis WeatherLink",
"link": "http:\/\/www.weatherlink.com"
},
"suggested_pickup": "15 minutes after the hour",
"suggested_pickup_period": "60",
"dewpoint_c": "-0.6",
"dewpoint_f": "31.0",
"dewpoint_string": "31.0 F (-0.6 C)",
"heat_index_c": "0.0",
"heat_index_f": "32.0",
"heat_index_string": "32.0 F (0.0 C)",
"location": "Fryks\u00e5s, Dalarna, Sweden",
"latitude": "61.194282356502",
"longitude": "14.5285606384277",
"observation_time": "Last Updated on Dec 29 2024, 11:14 pm CET",
"observation_time_rfc822": "Sun, 29 Dec 2024 23:14:45 +0100",
"pressure_in": "29.409",
"pressure_mb": "995.9",
"pressure_string": "995.9 mb",
"relative_humidity": "97",
"station_id": "petman",
"temp_c": "0.0",
"temp_f": "32.0",
"temperature_string": "32.0 F (0.0 C)",
"wind_degrees": "145",
"wind_dir": "Southeast",
"wind_kt": "5.2",
"wind_mph": "6.0",
"windchill_c": "-2.8",
"windchill_f": "27.0",
"windchill_string": "27.0 F (-2.8 C)",
"davis_current_observation": {
"DID": "001D0A0028EA",
"station_name": "Fryks\u00e5s",
"observation_age": 16,
"dewpoint_day_high_f": "32",
"dewpoint_day_high_time": "12:00am",
"dewpoint_day_low_f": "31",
"dewpoint_day_low_time": "12:00am",
"dewpoint_month_high_f": "43",
"dewpoint_month_low_f": "13",
"dewpoint_year_high_f": "70",
"dewpoint_year_low_f": "-4",
"heat_index_day_high_f": "32",
"heat_index_day_high_time": "12:00am",
"heat_index_month_high_f": "45",
"heat_index_year_high_f": "52",
"pressure_day_high_in": "29.409",
"pressure_day_high_time": "12:00am",
"pressure_day_low_in": "29.406",
"pressure_day_low_time": "12:04am",
"pressure_month_high_in": "30.777",
"pressure_month_low_in": "28.841",
"pressure_tendency_string": "Falling Slowly",
"pressure_year_high_in": "30.777",
"pressure_year_low_in": "28.615",
"rain_day_in": "0.0000",
"rain_month_in": "0.4882",
"rain_rate_day_high_in_per_hr": "0.0000",
"rain_rate_hour_high_in_per_hr": "0.0000",
"rain_rate_in_per_hr": "0.0000",
"rain_rate_month_high_in_per_hr": "0.1260",
"rain_rate_year_high_in_per_hr": "5.9685",
"rain_storm_in": "0.0157",
"rain_storm_start_date": "12\/13\/2024",
"rain_year_in": "28.4094",
"relative_humidity_day_high": "97",
"relative_humidity_day_high_time": "12:00am",
"relative_humidity_day_low": "97",
"relative_humidity_day_low_time": "12:00am",
"relative_humidity_in": "40",
"relative_humidity_in_day_high": "40",
"relative_humidity_in_day_high_time": "12:00am",
"relative_humidity_in_day_low": "40",
"relative_humidity_in_day_low_time": "12:00am",
"relative_humidity_in_month_high": "50",
"relative_humidity_in_month_low": "31",
"relative_humidity_in_year_high": "61",
"relative_humidity_in_year_low": "19",
"sunrise": "10:17am",
"sunset": "3:54pm",
"temp_day_high_f": "32.2",
"temp_day_high_time": "12:00am",
"temp_day_low_f": "32.0",
"temp_day_low_time": "12:14am",
"temp_month_high_f": "44.6",
"temp_month_low_f": "14.1",
"temp_year_high_f": "79.2",
"temp_year_low_f": "-1.0",
"temp_in_day_high_f": "71.4",
"temp_in_day_high_time": "12:00am",
"temp_in_day_low_f": "70.7",
"temp_in_day_low_time": "12:14am",
"temp_in_f": "70.7",
"temp_in_month_high_f": "75.3",
"temp_in_month_low_f": "54.0",
"temp_in_year_high_f": "80.0",
"temp_in_year_low_f": "52.0",
"wind_day_high_mph": "12.0",
"wind_day_high_time": "12:14am",
"wind_month_high_mph": "29.0",
"wind_ten_min_avg_mph": "4.0",
"wind_ten_min_gust_mph": "12.0",
"wind_year_high_mph": "42.0",
"windchill_day_low_f": "27",
"windchill_day_low_time": "12:00am",
"windchill_month_low_f": "10",
"windchill_year_low_f": "3"
},
"time_to_generate": "0.020508 seconds"
}
8 changes: 4 additions & 4 deletions tests/snapshots/test_binary_sensor.ambr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# serializer version: 1
# name: test_sensor[binary_sensor.strp81_connectivity-entry]
# name: test_binary_sensor[binary_sensor.strp81_connectivity-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
Expand Down Expand Up @@ -32,7 +32,7 @@
'unit_of_measurement': None,
})
# ---
# name: test_sensor[binary_sensor.strp81_connectivity-state]
# name: test_binary_sensor[binary_sensor.strp81_connectivity-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'connectivity',
Expand All @@ -47,7 +47,7 @@
'state': 'off',
})
# ---
# name: test_sensor[binary_sensor.strp81_transmitter_battery-entry]
# name: test_binary_sensor[binary_sensor.strp81_transmitter_battery-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
Expand Down Expand Up @@ -80,7 +80,7 @@
'unit_of_measurement': None,
})
# ---
# name: test_sensor[binary_sensor.strp81_transmitter_battery-state]
# name: test_binary_sensor[binary_sensor.strp81_transmitter_battery-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'battery',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_sensor(
async def test_binary_sensor(
hass: HomeAssistant,
bypass_get_data,
bypass_get_station,
Expand Down
Loading

0 comments on commit d3bf0b3

Please sign in to comment.