Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions homeassistant/components/buienradar/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@

CONF_DIMENSION = "dimension"
CONF_DELTA = "delta"
CONF_COUNTRY = "country_code"

RADAR_MAP_URL_TEMPLATE = "https://api.buienradar.nl/image/1.0/RadarMapNL?w={w}&h={h}"
RADAR_MAP_URL_TEMPLATE = "https://api.buienradar.nl/image/1.0/RadarMap{c}?w={w}&h={h}"

_LOG = logging.getLogger(__name__)

# Maximum range according to docs
DIM_RANGE = vol.All(vol.Coerce(int), vol.Range(min=120, max=700))

# Multiple choice for available Radar Map URL
SUPPORTED_COUNTRY_CODES = ["NL", "BE"]

PLATFORM_SCHEMA = vol.All(
PLATFORM_SCHEMA.extend(
{
Expand All @@ -31,6 +35,9 @@
vol.Coerce(float), vol.Range(min=0)
),
vol.Optional(CONF_NAME, default="Buienradar loop"): cv.string,
vol.Optional(CONF_COUNTRY, default="NL"): vol.All(
vol.Coerce(str), vol.In(SUPPORTED_COUNTRY_CODES)
),
}
)
)
Expand All @@ -41,8 +48,9 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
dimension = config[CONF_DIMENSION]
delta = config[CONF_DELTA]
name = config[CONF_NAME]
country = config[CONF_COUNTRY]

async_add_entities([BuienradarCam(name, dimension, delta)])
async_add_entities([BuienradarCam(name, dimension, delta, country)])


class BuienradarCam(Camera):
Expand All @@ -54,7 +62,7 @@ class BuienradarCam(Camera):
[0]: https://www.buienradar.nl/overbuienradar/gratis-weerdata
"""

def __init__(self, name: str, dimension: int, delta: float):
def __init__(self, name: str, dimension: int, delta: float, country: str):
"""
Initialize the component.

Expand All @@ -70,6 +78,9 @@ def __init__(self, name: str, dimension: int, delta: float):
# time a cached image stays valid for
self._delta = delta

# country location
self._country = country

# Condition that guards the loading indicator.
#
# Ensures that only one reader can cause an http request at the same
Expand Down Expand Up @@ -101,7 +112,9 @@ async def __retrieve_radar_image(self) -> bool:
"""Retrieve new radar image and return whether this succeeded."""
session = async_get_clientsession(self.hass)

url = RADAR_MAP_URL_TEMPLATE.format(w=self._dimension, h=self._dimension)
url = RADAR_MAP_URL_TEMPLATE.format(
c=self._country, w=self._dimension, h=self._dimension
)

if self._last_modified:
headers = {"If-Modified-Since": self._last_modified}
Expand Down
29 changes: 25 additions & 4 deletions tests/components/buienradar/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
EPSILON_DELTA = 0.0000000001


def radar_map_url(dim: int = 512) -> str:
def radar_map_url(dim: int = 512, country_code: str = "NL") -> str:
"""Build map url, defaulting to 512 wide (as in component)."""
return ("https://api.buienradar.nl/image/1.0/RadarMapNL?w={dim}&h={dim}").format(
dim=dim
)
return f"https://api.buienradar.nl/image/1.0/RadarMap{country_code}?w={dim}&h={dim}"


async def test_fetching_url_and_caching(aioclient_mock, hass, hass_client):
Expand Down Expand Up @@ -110,6 +108,29 @@ async def test_dimension(aioclient_mock, hass, hass_client):
assert aioclient_mock.call_count == 1


async def test_belgium_country(aioclient_mock, hass, hass_client):
"""Test that it actually adheres to another country like Belgium."""
aioclient_mock.get(radar_map_url(country_code="BE"), text="hello world")

await async_setup_component(
hass,
"camera",
{
"camera": {
"name": "config_test",
"platform": "buienradar",
"country_code": "BE",
}
},
)

client = await hass_client()

await client.get("/api/camera_proxy/camera.config_test")

assert aioclient_mock.call_count == 1


async def test_failure_response_not_cached(aioclient_mock, hass, hass_client):
"""Test that it does not cache a failure response."""
aioclient_mock.get(radar_map_url(), text="hello world", status=401)
Expand Down