Skip to content

Commit

Permalink
Merge pull request #9 from jdejaegh/high-low-temp
Browse files Browse the repository at this point in the history
Ensure native_templow <= native_temperature
  • Loading branch information
jdejaegh authored Jan 22, 2024
2 parents 21abf64 + 11d055e commit aa5c0e5
Show file tree
Hide file tree
Showing 6 changed files with 1,710 additions and 2 deletions.
1 change: 0 additions & 1 deletion custom_components/irm_kmi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from homeassistant.components.weather import (ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_CLOUDY,
ATTR_CONDITION_EXCEPTIONAL,
ATTR_CONDITION_FOG,
ATTR_CONDITION_LIGHTNING_RAINY,
ATTR_CONDITION_PARTLYCLOUDY,
Expand Down
7 changes: 7 additions & 0 deletions custom_components/irm_kmi/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ def daily_list_to_forecast(self, data: List[dict] | None) -> List[Forecast] | No
is_daytime=is_daytime,
text=f.get('text', {}).get(self.hass.config.language, ""),
)
# Swap temperature and templow if needed
if (forecast['native_templow'] is not None
and forecast['native_temperature'] is not None
and forecast['native_templow'] > forecast['native_temperature']):
(forecast['native_templow'], forecast['native_temperature']) = \
(forecast['native_temperature'], forecast['native_templow'])

forecasts.append(forecast)
if is_daytime or idx == 0:
n_days += 1
Expand Down
8 changes: 8 additions & 0 deletions custom_components/irm_kmi/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,16 @@ def daily_forecast(self) -> list[Forecast] | None:
return None
if len(data) > 1 and not data[0].get('is_daytime') and data[1].get('native_templow') is None:
data[1]['native_templow'] = data[0].get('native_templow')
if data[1]['native_templow'] > data[1]['native_temperature']:
(data[1]['native_templow'], data[1]['native_temperature']) = \
(data[1]['native_temperature'], data[1]['native_templow'])

if len(data) > 0 and not data[0].get('is_daytime'):
return data
if len(data) > 1 and data[0].get('native_templow') is None and not data[1].get('is_daytime'):
data[0]['native_templow'] = data[1].get('native_templow')
if data[0]['native_templow'] > data[0]['native_temperature']:
(data[0]['native_templow'], data[0]['native_temperature']) = \
(data[0]['native_temperature'], data[0]['native_templow'])

return [f for f in data if f.get('is_daytime')]
18 changes: 17 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def patched(url: str, params: dict | None = None) -> bytes:
elif "getLocalizationLayerNL" in url:
file_name = "tests/fixtures/loc_layer_nl.png"
else:
raise ValueError("Not a valid parameter for the mock")
raise ValueError(f"Not a valid parameter for the mock: {url}")

with open(file_name, "rb") as file:
return file.read()
Expand Down Expand Up @@ -180,6 +180,22 @@ def mock_image_and_nl_forecast_irm_kmi_api(request: pytest.FixtureRequest) -> Ge
yield irm_kmi


@pytest.fixture()
def mock_image_and_high_temp_irm_kmi_api(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
"""Return a mocked IrmKmi api client."""
fixture: str = "high_low_temp.json"

forecast = json.loads(load_fixture(fixture))

with patch(
"custom_components.irm_kmi.coordinator.IrmKmiApiClient", autospec=True
) as irm_kmi_api_mock:
irm_kmi = irm_kmi_api_mock.return_value
irm_kmi.get_image.side_effect = patched
irm_kmi.get_forecasts_coord.return_value = forecast
yield irm_kmi


@pytest.fixture()
def mock_coordinator(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
"""Return a mocked coordinator."""
Expand Down
Loading

0 comments on commit aa5c0e5

Please sign in to comment.