Skip to content

Commit

Permalink
Add clear night to smhi (#115998)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST authored May 22, 2024
1 parent 5c9c71b commit d1bdf73
Show file tree
Hide file tree
Showing 5 changed files with 840 additions and 4 deletions.
13 changes: 11 additions & 2 deletions homeassistant/components/smhi/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from smhi.smhi_lib import SmhiForecast, SmhiForecastException

from homeassistant.components.weather import (
ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_CLOUDY,
ATTR_CONDITION_EXCEPTIONAL,
ATTR_CONDITION_FOG,
Expand Down Expand Up @@ -55,11 +56,11 @@
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers import aiohttp_client, sun
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_call_later
from homeassistant.util import Throttle, slugify
from homeassistant.util import Throttle, dt as dt_util, slugify

from .const import ATTR_SMHI_THUNDER_PROBABILITY, DOMAIN, ENTITY_ID_SENSOR_FORMAT

Expand Down Expand Up @@ -189,6 +190,10 @@ async def async_update(self) -> None:
self._attr_native_wind_gust_speed = self._forecast_daily[0].wind_gust
self._attr_cloud_coverage = self._forecast_daily[0].cloudiness
self._attr_condition = CONDITION_MAP.get(self._forecast_daily[0].symbol)
if self._attr_condition == ATTR_CONDITION_SUNNY and not sun.is_up(
self.hass
):
self._attr_condition = ATTR_CONDITION_CLEAR_NIGHT
await self.async_update_listeners(("daily", "hourly"))

async def retry_update(self, _: datetime) -> None:
Expand All @@ -206,6 +211,10 @@ def _get_forecast_data(

for forecast in forecast_data[1:]:
condition = CONDITION_MAP.get(forecast.symbol)
if condition == ATTR_CONDITION_SUNNY and not sun.is_up(
self.hass, forecast.valid_time.replace(tzinfo=dt_util.UTC)
):
condition = ATTR_CONDITION_CLEAR_NIGHT

data.append(
{
Expand Down
6 changes: 6 additions & 0 deletions tests/components/smhi/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def api_response():
return load_fixture("smhi.json", DOMAIN)


@pytest.fixture(scope="package")
def api_response_night():
"""Return an API response for night only."""
return load_fixture("smhi_night.json", DOMAIN)


@pytest.fixture(scope="package")
def api_response_lack_data():
"""Return an API response."""
Expand Down
Loading

0 comments on commit d1bdf73

Please sign in to comment.