mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
Add Buienradar camera for Belgium (#30399)
* Buienradar Camera for Belgium * Voluptuous check for country code * Black formatting * Testing for Buienradar Belgium * Changed MULTIPLE CHOICE variable name * Changes from frenck review
This commit is contained in:
parent
b4f3415eb9
commit
6ff572d1dd
@ -15,14 +15,18 @@ from homeassistant.util import dt as dt_util
|
|||||||
|
|
||||||
CONF_DIMENSION = "dimension"
|
CONF_DIMENSION = "dimension"
|
||||||
CONF_DELTA = "delta"
|
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__)
|
_LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Maximum range according to docs
|
# Maximum range according to docs
|
||||||
DIM_RANGE = vol.All(vol.Coerce(int), vol.Range(min=120, max=700))
|
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 = vol.All(
|
||||||
PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
@ -31,6 +35,9 @@ PLATFORM_SCHEMA = vol.All(
|
|||||||
vol.Coerce(float), vol.Range(min=0)
|
vol.Coerce(float), vol.Range(min=0)
|
||||||
),
|
),
|
||||||
vol.Optional(CONF_NAME, default="Buienradar loop"): cv.string,
|
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)
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -41,8 +48,9 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
dimension = config[CONF_DIMENSION]
|
dimension = config[CONF_DIMENSION]
|
||||||
delta = config[CONF_DELTA]
|
delta = config[CONF_DELTA]
|
||||||
name = config[CONF_NAME]
|
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):
|
class BuienradarCam(Camera):
|
||||||
@ -54,7 +62,7 @@ class BuienradarCam(Camera):
|
|||||||
[0]: https://www.buienradar.nl/overbuienradar/gratis-weerdata
|
[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.
|
Initialize the component.
|
||||||
|
|
||||||
@ -70,6 +78,9 @@ class BuienradarCam(Camera):
|
|||||||
# time a cached image stays valid for
|
# time a cached image stays valid for
|
||||||
self._delta = delta
|
self._delta = delta
|
||||||
|
|
||||||
|
# country location
|
||||||
|
self._country = country
|
||||||
|
|
||||||
# Condition that guards the loading indicator.
|
# Condition that guards the loading indicator.
|
||||||
#
|
#
|
||||||
# Ensures that only one reader can cause an http request at the same
|
# Ensures that only one reader can cause an http request at the same
|
||||||
@ -101,7 +112,9 @@ class BuienradarCam(Camera):
|
|||||||
"""Retrieve new radar image and return whether this succeeded."""
|
"""Retrieve new radar image and return whether this succeeded."""
|
||||||
session = async_get_clientsession(self.hass)
|
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:
|
if self._last_modified:
|
||||||
headers = {"If-Modified-Since": self._last_modified}
|
headers = {"If-Modified-Since": self._last_modified}
|
||||||
|
@ -10,11 +10,9 @@ from homeassistant.util import dt as dt_util
|
|||||||
EPSILON_DELTA = 0.0000000001
|
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)."""
|
"""Build map url, defaulting to 512 wide (as in component)."""
|
||||||
return ("https://api.buienradar.nl/image/1.0/RadarMapNL?w={dim}&h={dim}").format(
|
return f"https://api.buienradar.nl/image/1.0/RadarMap{country_code}?w={dim}&h={dim}"
|
||||||
dim=dim
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_fetching_url_and_caching(aioclient_mock, hass, hass_client):
|
async def test_fetching_url_and_caching(aioclient_mock, hass, hass_client):
|
||||||
@ -110,6 +108,29 @@ async def test_dimension(aioclient_mock, hass, hass_client):
|
|||||||
assert aioclient_mock.call_count == 1
|
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):
|
async def test_failure_response_not_cached(aioclient_mock, hass, hass_client):
|
||||||
"""Test that it does not cache a failure response."""
|
"""Test that it does not cache a failure response."""
|
||||||
aioclient_mock.get(radar_map_url(), text="hello world", status=401)
|
aioclient_mock.get(radar_map_url(), text="hello world", status=401)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user