Added more attributes (#67135)

This commit is contained in:
Maximilian 2022-03-29 10:34:09 +02:00 committed by GitHub
parent 2bb42f48aa
commit d655d54a8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 24 deletions

View file

@ -1,7 +1,6 @@
"""The Nina integration."""
from __future__ import annotations
import datetime as dt
from typing import Any
from async_timeout import timeout
@ -12,14 +11,16 @@ from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util import dt as dt_util
from .const import (
_LOGGER,
ATTR_DESCRIPTION,
ATTR_EXPIRES,
ATTR_HEADLINE,
ATTR_ID,
ATTR_SENDER,
ATTR_SENT,
ATTR_SEVERITY,
ATTR_START,
CONF_FILTER_CORONA,
CONF_REGIONS,
@ -89,22 +90,15 @@ class NINADataUpdateCoordinator(DataUpdateCoordinator):
warn_obj: dict[str, Any] = {
ATTR_ID: raw_warn.id,
ATTR_HEADLINE: raw_warn.headline,
ATTR_SENT: self._to_utc(raw_warn.sent),
ATTR_START: self._to_utc(raw_warn.start),
ATTR_EXPIRES: self._to_utc(raw_warn.expires),
ATTR_DESCRIPTION: raw_warn.description,
ATTR_SENDER: raw_warn.sender,
ATTR_SEVERITY: raw_warn.severity,
ATTR_SENT: raw_warn.sent or "",
ATTR_START: raw_warn.start or "",
ATTR_EXPIRES: raw_warn.expires or "",
}
warnings_for_regions.append(warn_obj)
return_data[region_id] = warnings_for_regions
return return_data
@staticmethod
def _to_utc(input_time: str) -> str | None:
if input_time:
return (
dt.datetime.fromisoformat(input_time)
.astimezone(dt_util.UTC)
.isoformat()
)
return None

View file

@ -14,10 +14,13 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import NINADataUpdateCoordinator
from .const import (
ATTR_DESCRIPTION,
ATTR_EXPIRES,
ATTR_HEADLINE,
ATTR_ID,
ATTR_SENDER,
ATTR_SENT,
ATTR_SEVERITY,
ATTR_START,
CONF_MESSAGE_SLOTS,
CONF_REGIONS,
@ -83,6 +86,9 @@ class NINAMessage(CoordinatorEntity[NINADataUpdateCoordinator], BinarySensorEnti
return {
ATTR_HEADLINE: data[ATTR_HEADLINE],
ATTR_DESCRIPTION: data[ATTR_DESCRIPTION],
ATTR_SENDER: data[ATTR_SENDER],
ATTR_SEVERITY: data[ATTR_SEVERITY],
ATTR_ID: data[ATTR_ID],
ATTR_SENT: data[ATTR_SENT],
ATTR_START: data[ATTR_START],

View file

@ -16,6 +16,9 @@ CONF_MESSAGE_SLOTS: str = "slots"
CONF_FILTER_CORONA: str = "corona_filter"
ATTR_HEADLINE: str = "Headline"
ATTR_DESCRIPTION: str = "Description"
ATTR_SENDER: str = "Sender"
ATTR_SEVERITY: str = "Severity"
ATTR_ID: str = "ID"
ATTR_SENT: str = "Sent"
ATTR_START: str = "Start"

View file

@ -1,13 +1,18 @@
"""Test the Nina binary sensor."""
from __future__ import annotations
from typing import Any
from unittest.mock import patch
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.components.nina.const import (
ATTR_DESCRIPTION,
ATTR_EXPIRES,
ATTR_HEADLINE,
ATTR_ID,
ATTR_SENDER,
ATTR_SENT,
ATTR_SEVERITY,
ATTR_START,
DOMAIN,
)
@ -58,10 +63,16 @@ async def test_sensors(hass: HomeAssistant) -> None:
assert state_w1.state == STATE_ON
assert state_w1.attributes.get(ATTR_HEADLINE) == "Ausfall Notruf 112"
assert (
state_w1.attributes.get(ATTR_DESCRIPTION)
== "Es treten Sturmböen mit Geschwindigkeiten zwischen 70 km/h (20m/s, 38kn, Bft 8) und 85 km/h (24m/s, 47kn, Bft 9) aus westlicher Richtung auf. In Schauernähe sowie in exponierten Lagen muss mit schweren Sturmböen bis 90 km/h (25m/s, 48kn, Bft 10) gerechnet werden."
)
assert state_w1.attributes.get(ATTR_SENDER) == "Deutscher Wetterdienst"
assert state_w1.attributes.get(ATTR_SEVERITY) == "Minor"
assert state_w1.attributes.get(ATTR_ID) == "mow.DE-NW-BN-SE030-20201014-30-000"
assert state_w1.attributes.get(ATTR_SENT) == "2021-10-11T04:20:00+00:00"
assert state_w1.attributes.get(ATTR_START) == "2021-11-01T04:20:00+00:00"
assert state_w1.attributes.get(ATTR_EXPIRES) == "3021-11-22T04:19:00+00:00"
assert state_w1.attributes.get(ATTR_SENT) == "2021-10-11T05:20:00+01:00"
assert state_w1.attributes.get(ATTR_START) == "2021-11-01T05:20:00+01:00"
assert state_w1.attributes.get(ATTR_EXPIRES) == "3021-11-22T05:19:00+01:00"
assert entry_w1.unique_id == "083350000000-1"
assert state_w1.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY
@ -71,6 +82,9 @@ async def test_sensors(hass: HomeAssistant) -> None:
assert state_w2.state == STATE_OFF
assert state_w2.attributes.get(ATTR_HEADLINE) is None
assert state_w2.attributes.get(ATTR_DESCRIPTION) is None
assert state_w2.attributes.get(ATTR_SENDER) is None
assert state_w2.attributes.get(ATTR_SEVERITY) is None
assert state_w2.attributes.get(ATTR_ID) is None
assert state_w2.attributes.get(ATTR_SENT) is None
assert state_w2.attributes.get(ATTR_START) is None
@ -84,6 +98,9 @@ async def test_sensors(hass: HomeAssistant) -> None:
assert state_w3.state == STATE_OFF
assert state_w3.attributes.get(ATTR_HEADLINE) is None
assert state_w3.attributes.get(ATTR_DESCRIPTION) is None
assert state_w3.attributes.get(ATTR_SENDER) is None
assert state_w3.attributes.get(ATTR_SEVERITY) is None
assert state_w3.attributes.get(ATTR_ID) is None
assert state_w3.attributes.get(ATTR_SENT) is None
assert state_w3.attributes.get(ATTR_START) is None
@ -97,6 +114,9 @@ async def test_sensors(hass: HomeAssistant) -> None:
assert state_w4.state == STATE_OFF
assert state_w4.attributes.get(ATTR_HEADLINE) is None
assert state_w4.attributes.get(ATTR_DESCRIPTION) is None
assert state_w4.attributes.get(ATTR_SENDER) is None
assert state_w4.attributes.get(ATTR_SEVERITY) is None
assert state_w4.attributes.get(ATTR_ID) is None
assert state_w4.attributes.get(ATTR_SENT) is None
assert state_w4.attributes.get(ATTR_START) is None
@ -110,6 +130,9 @@ async def test_sensors(hass: HomeAssistant) -> None:
assert state_w5.state == STATE_OFF
assert state_w5.attributes.get(ATTR_HEADLINE) is None
assert state_w5.attributes.get(ATTR_DESCRIPTION) is None
assert state_w5.attributes.get(ATTR_SENDER) is None
assert state_w5.attributes.get(ATTR_SEVERITY) is None
assert state_w5.attributes.get(ATTR_ID) is None
assert state_w5.attributes.get(ATTR_SENT) is None
assert state_w5.attributes.get(ATTR_START) is None
@ -147,10 +170,16 @@ async def test_sensors_without_corona_filter(hass: HomeAssistant) -> None:
state_w1.attributes.get(ATTR_HEADLINE)
== "Corona-Verordnung des Landes: Warnstufe durch Landesgesundheitsamt ausgerufen"
)
assert (
state_w1.attributes.get(ATTR_DESCRIPTION)
== "Die Zahl der mit dem Corona-Virus infizierten Menschen steigt gegenwärtig stark an. Es wächst daher die Gefahr einer weiteren Verbreitung der Infektion und - je nach Einzelfall - auch von schweren Erkrankungen."
)
assert state_w1.attributes.get(ATTR_SENDER) == ""
assert state_w1.attributes.get(ATTR_SEVERITY) == "Minor"
assert state_w1.attributes.get(ATTR_ID) == "mow.DE-BW-S-SE018-20211102-18-001"
assert state_w1.attributes.get(ATTR_SENT) == "2021-11-02T19:07:16+00:00"
assert state_w1.attributes.get(ATTR_START) is None
assert state_w1.attributes.get(ATTR_EXPIRES) is None
assert state_w1.attributes.get(ATTR_SENT) == "2021-11-02T20:07:16+01:00"
assert state_w1.attributes.get(ATTR_START) == ""
assert state_w1.attributes.get(ATTR_EXPIRES) == ""
assert entry_w1.unique_id == "083350000000-1"
assert state_w1.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY
@ -160,10 +189,16 @@ async def test_sensors_without_corona_filter(hass: HomeAssistant) -> None:
assert state_w2.state == STATE_ON
assert state_w2.attributes.get(ATTR_HEADLINE) == "Ausfall Notruf 112"
assert (
state_w2.attributes.get(ATTR_DESCRIPTION)
== "Es treten Sturmböen mit Geschwindigkeiten zwischen 70 km/h (20m/s, 38kn, Bft 8) und 85 km/h (24m/s, 47kn, Bft 9) aus westlicher Richtung auf. In Schauernähe sowie in exponierten Lagen muss mit schweren Sturmböen bis 90 km/h (25m/s, 48kn, Bft 10) gerechnet werden."
)
assert state_w2.attributes.get(ATTR_SENDER) == "Deutscher Wetterdienst"
assert state_w2.attributes.get(ATTR_SEVERITY) == "Minor"
assert state_w2.attributes.get(ATTR_ID) == "mow.DE-NW-BN-SE030-20201014-30-000"
assert state_w2.attributes.get(ATTR_SENT) == "2021-10-11T04:20:00+00:00"
assert state_w2.attributes.get(ATTR_START) == "2021-11-01T04:20:00+00:00"
assert state_w2.attributes.get(ATTR_EXPIRES) == "3021-11-22T04:19:00+00:00"
assert state_w2.attributes.get(ATTR_SENT) == "2021-10-11T05:20:00+01:00"
assert state_w2.attributes.get(ATTR_START) == "2021-11-01T05:20:00+01:00"
assert state_w2.attributes.get(ATTR_EXPIRES) == "3021-11-22T05:19:00+01:00"
assert entry_w2.unique_id == "083350000000-2"
assert state_w2.attributes.get("device_class") == BinarySensorDeviceClass.SAFETY
@ -173,6 +208,9 @@ async def test_sensors_without_corona_filter(hass: HomeAssistant) -> None:
assert state_w3.state == STATE_OFF
assert state_w3.attributes.get(ATTR_HEADLINE) is None
assert state_w3.attributes.get(ATTR_DESCRIPTION) is None
assert state_w3.attributes.get(ATTR_SENDER) is None
assert state_w3.attributes.get(ATTR_SEVERITY) is None
assert state_w3.attributes.get(ATTR_ID) is None
assert state_w3.attributes.get(ATTR_SENT) is None
assert state_w3.attributes.get(ATTR_START) is None
@ -186,6 +224,9 @@ async def test_sensors_without_corona_filter(hass: HomeAssistant) -> None:
assert state_w4.state == STATE_OFF
assert state_w4.attributes.get(ATTR_HEADLINE) is None
assert state_w4.attributes.get(ATTR_DESCRIPTION) is None
assert state_w4.attributes.get(ATTR_SENDER) is None
assert state_w4.attributes.get(ATTR_SEVERITY) is None
assert state_w4.attributes.get(ATTR_ID) is None
assert state_w4.attributes.get(ATTR_SENT) is None
assert state_w4.attributes.get(ATTR_START) is None
@ -199,6 +240,9 @@ async def test_sensors_without_corona_filter(hass: HomeAssistant) -> None:
assert state_w5.state == STATE_OFF
assert state_w5.attributes.get(ATTR_HEADLINE) is None
assert state_w5.attributes.get(ATTR_DESCRIPTION) is None
assert state_w5.attributes.get(ATTR_SENDER) is None
assert state_w5.attributes.get(ATTR_SEVERITY) is None
assert state_w5.attributes.get(ATTR_ID) is None
assert state_w5.attributes.get(ATTR_SENT) is None
assert state_w5.attributes.get(ATTR_START) is None