Skip to content

Commit 175163c

Browse files
authored
Merge pull request #27 from golles/formatting_and_linting
Improvements from black and pylint
2 parents 9c08f55 + a4d5165 commit 175163c

12 files changed

+69
-70
lines changed

custom_components/knmi/__init__.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@
1616
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
1717

1818
from .api import KnmiApiClient
19-
20-
from .const import (
21-
DOMAIN,
22-
PLATFORMS,
23-
)
19+
from .const import DOMAIN, PLATFORMS
2420

2521
SCAN_INTERVAL = timedelta(seconds=300)
2622

2723
_LOGGER: logging.Logger = logging.getLogger(__package__)
2824

2925

30-
async def async_setup(hass: HomeAssistant, config: Config):
26+
async def async_setup(_hass: HomeAssistant, _config: Config):
3127
"""Set up this integration using YAML is not supported."""
3228
return True
3329

@@ -37,12 +33,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
3733
if hass.data.get(DOMAIN) is None:
3834
hass.data.setdefault(DOMAIN, {})
3935

40-
apiKey = entry.data.get(CONF_API_KEY)
36+
api_key = entry.data.get(CONF_API_KEY)
4137
latitude = entry.data.get(CONF_LATITUDE)
4238
longitude = entry.data.get(CONF_LONGITUDE)
4339

4440
session = async_get_clientsession(hass)
45-
client = KnmiApiClient(apiKey, latitude, longitude, session)
41+
client = KnmiApiClient(api_key, latitude, longitude, session)
4642

4743
coordinator = KnmiDataUpdateCoordinator(hass, client=client)
4844
await coordinator.async_refresh()
@@ -66,9 +62,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
6662
class KnmiDataUpdateCoordinator(DataUpdateCoordinator):
6763
"""Class to manage fetching data from the API."""
6864

69-
def __init__(
70-
self, hass: HomeAssistant, client: KnmiApiClient
71-
) -> None:
65+
def __init__(self, hass: HomeAssistant, client: KnmiApiClient) -> None:
7266
"""Initialize."""
7367
self.api = client
7468
self.platforms = []

custom_components/knmi/api.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,40 @@
22
import logging
33
import asyncio
44
import socket
5-
from typing import Optional
65
import aiohttp
76
import async_timeout
87

9-
TIMEOUT = 10
8+
from .const import API_ENDPOINT, API_TIMEOUT
109

1110

1211
_LOGGER: logging.Logger = logging.getLogger(__package__)
1312

1413

1514
class KnmiApiClient:
15+
"""KNMI API wrapper"""
16+
1617
def __init__(
17-
self, apiKey: str, latitude: str, longitude: str, session: aiohttp.ClientSession
18+
self,
19+
api_key: str,
20+
latitude: str,
21+
longitude: str,
22+
session: aiohttp.ClientSession,
1823
) -> None:
1924
"""Sample API Client."""
20-
self.apiKey = apiKey
25+
self.api_key = api_key
2126
self.latitude = latitude
2227
self.longitude = longitude
2328
self._session = session
2429

2530
async def async_get_data(self) -> dict:
2631
"""Get data from the API."""
27-
url = f"http://weerlive.nl/api/json-data-10min.php?key={self.apiKey}&locatie={self.latitude},{self.longitude}"
32+
url = API_ENDPOINT.format(self.api_key, self.latitude, self.longitude)
2833
return await self.api_wrapper("get", url)
2934

3035
async def api_wrapper(self, method: str, url: str) -> dict:
3136
"""Get information from the API."""
3237
try:
33-
async with async_timeout.timeout(TIMEOUT):
38+
async with async_timeout.timeout(API_TIMEOUT):
3439
if method == "get":
3540
response = await self._session.get(url)
3641
# The API has no proper error handling for a wrong API key.
@@ -72,4 +77,4 @@ async def api_wrapper(self, method: str, url: str) -> dict:
7277

7378

7479
class KnmiApiException(Exception):
75-
pass
80+
"""KNMI API Exception class"""

custom_components/knmi/binary_sensor.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ def name(self):
5959
@property
6060
def is_on(self):
6161
"""Return true if the binary_sensor is on."""
62-
if super().getData(self._data_key) is not None:
63-
return super().getData(self._data_key) != "0"
62+
if super().get_data(self._data_key) is not None:
63+
return super().get_data(self._data_key) != "0"
64+
return None
6465

6566
@property
6667
def icon(self):
@@ -79,7 +80,7 @@ def extra_state_attributes(self):
7980
for attribute in self._attributes:
8081
value = None
8182
if "key" in attribute:
82-
value = super().getData(attribute.get("key", None))
83+
value = super().get_data(attribute.get("key", None))
8384
if "value" in attribute:
8485
value = attribute.get("value", None)
8586
attributes[attribute.get("name", None)] = value

custom_components/knmi/config_flow.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ async def _show_config_form(self, user_input): # pylint: disable=unused-argumen
7979
errors=self._errors,
8080
)
8181

82-
async def _test_user_input(self, apiKey: str, latitude: str, longitude: str):
82+
async def _test_user_input(self, api_key: str, latitude: str, longitude: str):
8383
"""Return true if credentials is valid."""
8484
try:
8585
session = async_create_clientsession(self.hass)
86-
client = KnmiApiClient(apiKey, latitude, longitude, session)
86+
client = KnmiApiClient(api_key, latitude, longitude, session)
8787
await client.async_get_data()
8888
return True
8989
except Exception: # pylint: disable=broad-except

custom_components/knmi/const.py

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
ATTR_CONDITION_SUNNY,
2525
)
2626

27+
# API
28+
API_ENDPOINT = "http://weerlive.nl/api/json-data-10min.php?key={}&locatie={},{}"
29+
API_TIMEOUT = 10
30+
2731
# Base component constants.
2832
NAME = "KNMI"
2933
DOMAIN = "knmi"

custom_components/knmi/entity.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77

88
class KnmiEntity(CoordinatorEntity):
9+
"""KNMI CoordinatorEntity"""
10+
911
def __init__(self, coordinator, config_entry):
1012
super().__init__(coordinator)
1113
self.coordinator = coordinator
1214
self.config_entry = config_entry
1315

14-
def getData(self, key):
16+
def get_data(self, key):
1517
"""Return the data key from the coordinator."""
1618
if self.coordinator.data is not None:
1719
return self.coordinator.data[key]

custom_components/knmi/sensor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def name(self):
5959
@property
6060
def native_value(self):
6161
"""Return the native_value of the sensor."""
62-
return super().getData(self._data_key)
62+
return super().get_data(self._data_key)
6363

6464
@property
6565
def native_unit_of_measurement(self):
@@ -83,7 +83,7 @@ def extra_state_attributes(self):
8383
for attribute in self._attributes:
8484
value = None
8585
if "key" in attribute:
86-
value = super().getData(attribute.get("key", None))
86+
value = super().get_data(attribute.get("key", None))
8787
if "value" in attribute:
8888
value = attribute.get("value", None)
8989
attributes[attribute.get("name", None)] = value

custom_components/knmi/weather.py

+28-28
Original file line numberDiff line numberDiff line change
@@ -41,50 +41,50 @@ def name(self):
4141
@property
4242
def condition(self):
4343
"""Return the current condition."""
44-
if super().getData("d0weer") is not None:
45-
return CONDITIONS_MAP[super().getData("d0weer")]
44+
if super().get_data("d0weer") is not None:
45+
return CONDITIONS_MAP[super().get_data("d0weer")]
4646
return None
4747

4848
@property
4949
def native_temperature(self):
5050
"""Return the temperature."""
51-
if super().getData("temp") is not None:
52-
return float(super().getData("temp"))
51+
if super().get_data("temp") is not None:
52+
return float(super().get_data("temp"))
5353
return None
5454

5555
@property
5656
def native_pressure(self):
5757
"""Return the pressure."""
58-
if super().getData("luchtd") is not None:
59-
return float(super().getData("luchtd"))
58+
if super().get_data("luchtd") is not None:
59+
return float(super().get_data("luchtd"))
6060
return None
6161

6262
@property
6363
def native_humidity(self):
6464
"""Return the humidity."""
65-
if super().getData("lv") is not None:
66-
return float(super().getData("lv"))
65+
if super().get_data("lv") is not None:
66+
return float(super().get_data("lv"))
6767
return None
6868

6969
@property
7070
def native_wind_speed(self):
7171
"""Return the wind speed."""
72-
if super().getData("windkmh") is not None:
73-
return float(super().getData("windkmh"))
72+
if super().get_data("windkmh") is not None:
73+
return float(super().get_data("windkmh"))
7474
return None
7575

7676
@property
7777
def wind_bearing(self):
7878
"""Return the wind direction."""
79-
if super().getData("windr") is not None:
80-
return WIND_DIRECTION_MAP[super().getData("windr")]
79+
if super().get_data("windr") is not None:
80+
return WIND_DIRECTION_MAP[super().get_data("windr")]
8181
return None
8282

8383
@property
8484
def native_visibility(self):
8585
"""Return the wind direction."""
86-
if super().getData("zicht") is not None:
87-
return float(super().getData("zicht"))
86+
if super().get_data("zicht") is not None:
87+
return float(super().get_data("zicht"))
8888
return None
8989

9090
@property
@@ -96,38 +96,38 @@ def forecast(self):
9696
for i in range(0, 3):
9797
date = today + timedelta(days=i)
9898
condition = (
99-
CONDITIONS_MAP[super().getData(f"d{i}weer")]
100-
if super().getData(f"d{i}weer") is not None
99+
CONDITIONS_MAP[super().get_data(f"d{i}weer")]
100+
if super().get_data(f"d{i}weer") is not None
101101
else None
102102
)
103103
wind_bearing = (
104-
WIND_DIRECTION_MAP[super().getData(f"d{i}windr")]
105-
if super().getData(f"d{i}windr") is not None
104+
WIND_DIRECTION_MAP[super().get_data(f"d{i}windr")]
105+
if super().get_data(f"d{i}windr") is not None
106106
else None
107107
)
108108
temp_low = (
109-
float(super().getData(f"d{i}tmin"))
110-
if super().getData(f"d{i}tmin") is not None
109+
float(super().get_data(f"d{i}tmin"))
110+
if super().get_data(f"d{i}tmin") is not None
111111
else None
112112
)
113113
temp = (
114-
float(super().getData(f"d{i}tmax"))
115-
if super().getData(f"d{i}tmax") is not None
114+
float(super().get_data(f"d{i}tmax"))
115+
if super().get_data(f"d{i}tmax") is not None
116116
else None
117117
)
118118
precipitation_probability = (
119-
float(super().getData(f"d{i}neerslag"))
120-
if super().getData(f"d{i}neerslag") is not None
119+
float(super().get_data(f"d{i}neerslag"))
120+
if super().get_data(f"d{i}neerslag") is not None
121121
else None
122122
)
123123
wind_speed = (
124-
float(super().getData(f"d{i}windkmh"))
125-
if super().getData(f"d{i}windkmh") is not None
124+
float(super().get_data(f"d{i}windkmh"))
125+
if super().get_data(f"d{i}windkmh") is not None
126126
else None
127127
)
128128
sun_chance = (
129-
float(super().getData(f"d{i}zon"))
130-
if super().getData(f"d{i}zon") is not None
129+
float(super().get_data(f"d{i}zon"))
130+
if super().get_data(f"d{i}zon") is not None
131131
else None
132132
)
133133
next_day = {

tests/conftest.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def skip_notifications_fixture():
4545
@pytest.fixture(name="bypass_get_data")
4646
def bypass_get_data_fixture():
4747
"""Skip calls to get data from API."""
48-
with patch(
49-
"custom_components.knmi.KnmiApiClient.async_get_data"
50-
):
48+
with patch("custom_components.knmi.KnmiApiClient.async_get_data"):
5149
yield
5250

5351

tests/test_api.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Tests for knmi api."""
2-
import asyncio
32

4-
import aiohttp
53
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
64
from homeassistant.helpers.aiohttp_client import async_get_clientsession
75

tests/test_config_flow.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
@pytest.fixture(autouse=True)
2424
def bypass_setup_fixture():
2525
"""Prevent setup."""
26-
with patch(
27-
"custom_components.knmi.async_setup",
28-
return_value=True,
29-
), patch(
26+
with patch("custom_components.knmi.async_setup", return_value=True,), patch(
3027
"custom_components.knmi.async_setup_entry",
3128
return_value=True,
3229
):
@@ -107,4 +104,8 @@ async def test_options_flow(hass):
107104
assert result["title"] == MOCK_CONFIG[CONF_NAME]
108105

109106
# Verify that the options were updated
110-
assert entry.options == {BINARY_SENSOR: True, SENSOR: True, WEATHER: False,}
107+
assert entry.options == {
108+
BINARY_SENSOR: True,
109+
SENSOR: True,
110+
WEATHER: False,
111+
}

tests/test_init.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,12 @@ async def test_setup_unload_and_reload_entry(hass, bypass_get_data):
2929
# call, no code from custom_components/knmi/api.py actually runs.
3030
assert await async_setup_entry(hass, config_entry)
3131
assert DOMAIN in hass.data and config_entry.entry_id in hass.data[DOMAIN]
32-
assert (
33-
type(hass.data[DOMAIN][config_entry.entry_id]) == KnmiDataUpdateCoordinator
34-
)
32+
assert type(hass.data[DOMAIN][config_entry.entry_id]) == KnmiDataUpdateCoordinator
3533

3634
# Reload the entry and assert that the data from above is still there
3735
assert await async_reload_entry(hass, config_entry) is None
3836
assert DOMAIN in hass.data and config_entry.entry_id in hass.data[DOMAIN]
39-
assert (
40-
type(hass.data[DOMAIN][config_entry.entry_id]) == KnmiDataUpdateCoordinator
41-
)
37+
assert type(hass.data[DOMAIN][config_entry.entry_id]) == KnmiDataUpdateCoordinator
4238

4339
# Unload the entry and verify that the data has been removed
4440
assert await async_unload_entry(hass, config_entry)

0 commit comments

Comments
 (0)