Skip to content

Commit

Permalink
Fix language setting causing sensor to fail. Fix #10
Browse files Browse the repository at this point in the history
  • Loading branch information
jdejaegh committed Feb 25, 2024
1 parent 274e53a commit e1c2e8a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion custom_components/irm_kmi/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ def extra_state_attributes(self) -> dict:
warning['is_active'] = warning.get('starts_at') < now < warning.get('ends_at')

attrs["active_warnings_friendly_names"] = ", ".join([warning['friendly_name'] for warning in attrs['warnings']
if warning['is_active']])
if warning['is_active'] and warning['friendly_name'] != ''])

return attrs
6 changes: 3 additions & 3 deletions custom_components/irm_kmi/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ async def _async_update_data(self) -> ProcessedCoordinatorData:
"""
if (zone := self.hass.states.get(self._zone)) is None:
raise UpdateFailed(f"Zone '{self._zone}' not found")

try:
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
# handled by the data update coordinator.
Expand Down Expand Up @@ -371,13 +370,14 @@ def warnings_from_data(self, warning_data: list | None) -> List[WarningData]:
except TypeError:
level = None

lang = self.hass.config.language if self.hass.config.language in LANGS else 'en'
result.append(
WarningData(
slug=SLUG_MAP.get(warning_id, 'unknown'),
id=warning_id,
level=level,
friendly_name=data.get('warningType', {}).get('name', {}).get(self.hass.config.language),
text=data.get('text', {}).get(self.hass.config.language),
friendly_name=data.get('warningType', {}).get('name', {}).get(lang, ''),
text=data.get('text', {}).get(lang, ''),
starts_at=start,
ends_at=end
)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,29 @@ async def test_warning_data(
assert w['is_active']

assert warning.extra_state_attributes['active_warnings_friendly_names'] == "Fog, Ice or snow"


@freeze_time(datetime.fromisoformat('2024-01-12T07:55:00+01:00'))
async def test_warning_data(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry
) -> None:
# When language is unknown, default to english setting
hass.config.language = "foo"

api_data = get_api_data("be_forecast_warning.json")
coordinator = IrmKmiCoordinator(hass, mock_config_entry)

result = coordinator.warnings_from_data(api_data.get('for', {}).get('warning'))

coordinator.data = {'warnings': result}
warning = IrmKmiWarning(coordinator, mock_config_entry)
warning.hass = hass

assert warning.is_on
assert len(warning.extra_state_attributes['warnings']) == 2

for w in warning.extra_state_attributes['warnings']:
assert w['is_active']

assert warning.extra_state_attributes['active_warnings_friendly_names'] == "Fog, Ice or snow"

0 comments on commit e1c2e8a

Please sign in to comment.