GeoNet NZ Volcanic Alert Level sensor (#26901)

* first version of new integration

* moved icon to shared consts

* added unit tests

* transformed from geolocation to sensor integration

* alert level is now the state of the sensor

* adopted unit tests

* fixed comment

* keep all sensors registered even if the feed update fails intermittently

* bumped version of upstream library

* bumped version of integration library

* regenerated requirements

* bumped version of integration library

* bumped version of integration library

* fixed generated file

* removed commented out code

* regenerated config flow file

* update to latest integration library version

* simplified code

* removed debug log statement

* simplified code structure

* defined constant

* use core interfaces

* moved test and fixture

* sorted imports

* simplified patching

* moved fixture to central config file
This commit is contained in:
Malte Franken authored and Martin Hjelmare committed 2019-11-19 14:00:59 +01:00
1 parent 8439e597ce
commit 4e9e9efa43
16 files changed
+844

No files matched your search

@@ -0,0 +1,25 @@
"""The tests for the GeoNet NZ Volcano Feed integration."""
from unittest.mock import MagicMock
def _generate_mock_feed_entry(
external_id,
title,
alert_level,
distance_to_home,
coordinates,
attribution=None,
activity=None,
hazards=None,
):
"""Construct a mock feed entry for testing purposes."""
feed_entry = MagicMock()
feed_entry.external_id = external_id
feed_entry.title = title
feed_entry.alert_level = alert_level
feed_entry.distance_to_home = distance_to_home
feed_entry.coordinates = coordinates
feed_entry.attribution = attribution
feed_entry.activity = activity
feed_entry.hazards = hazards
return feed_entry
@@ -0,0 +1,28 @@
"""Configuration for GeoNet NZ Volcano tests."""
import pytest
from homeassistant.components.geonetnz_volcano import DOMAIN
from homeassistant.const import (
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_RADIUS,
CONF_UNIT_SYSTEM,
CONF_SCAN_INTERVAL,
)
from tests.common import MockConfigEntry
@pytest.fixture
def config_entry():
"""Create a mock GeoNet NZ Volcano config entry."""
return MockConfigEntry(
domain=DOMAIN,
data={
CONF_LATITUDE: -41.2,
CONF_LONGITUDE: 174.7,
CONF_RADIUS: 25,
CONF_UNIT_SYSTEM: "metric",
CONF_SCAN_INTERVAL: 300.0,
},
title="-41.2, 174.7",
)
@@ -0,0 +1,81 @@
"""Define tests for the GeoNet NZ Volcano config flow."""
from datetime import timedelta
from homeassistant import data_entry_flow
from homeassistant.components.geonetnz_volcano import config_flow
from homeassistant.const import (
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_RADIUS,
CONF_SCAN_INTERVAL,
CONF_UNIT_SYSTEM,
)
async def test_duplicate_error(hass, config_entry):
"""Test that errors are shown when duplicates are added."""
conf = {CONF_LATITUDE: -41.2, CONF_LONGITUDE: 174.7, CONF_RADIUS: 25}
config_entry.add_to_hass(hass)
flow = config_flow.GeonetnzVolcanoFlowHandler()
flow.hass = hass
result = await flow.async_step_user(user_input=conf)
assert result["errors"] == {"base": "identifier_exists"}
async def test_show_form(hass):
"""Test that the form is served with no input."""
flow = config_flow.GeonetnzVolcanoFlowHandler()
flow.hass = hass
result = await flow.async_step_user(user_input=None)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
async def test_step_import(hass):
"""Test that the import step works."""
conf = {
CONF_LATITUDE: -41.2,
CONF_LONGITUDE: 174.7,
CONF_RADIUS: 25,
CONF_UNIT_SYSTEM: "metric",
CONF_SCAN_INTERVAL: timedelta(minutes=4),
}
flow = config_flow.GeonetnzVolcanoFlowHandler()
flow.hass = hass
result = await flow.async_step_import(import_config=conf)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "-41.2, 174.7"
assert result["data"] == {
CONF_LATITUDE: -41.2,
CONF_LONGITUDE: 174.7,
CONF_RADIUS: 25,
CONF_UNIT_SYSTEM: "metric",
CONF_SCAN_INTERVAL: 240.0,
}
async def test_step_user(hass):
"""Test that the user step works."""
hass.config.latitude = -41.2
hass.config.longitude = 174.7
conf = {CONF_RADIUS: 25}
flow = config_flow.GeonetnzVolcanoFlowHandler()
flow.hass = hass
result = await flow.async_step_user(user_input=conf)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "-41.2, 174.7"
assert result["data"] == {
CONF_LATITUDE: -41.2,
CONF_LONGITUDE: 174.7,
CONF_RADIUS: 25,
CONF_UNIT_SYSTEM: "metric",
CONF_SCAN_INTERVAL: 300.0,
}
@@ -0,0 +1,22 @@
"""Define tests for the GeoNet NZ Volcano general setup."""
from asynctest import CoroutineMock, patch
from homeassistant.components.geonetnz_volcano import DOMAIN, FEED
async def test_component_unload_config_entry(hass, config_entry):
"""Test that loading and unloading of a config entry works."""
config_entry.add_to_hass(hass)
with patch(
"aio_geojson_geonetnz_volcano.GeonetnzVolcanoFeedManager.update",
new_callable=CoroutineMock,
) as mock_feed_manager_update:
# Load config entry.
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert mock_feed_manager_update.call_count == 1
assert hass.data[DOMAIN][FEED][config_entry.entry_id] is not None
# Unload config entry.
assert await hass.config_entries.async_unload(config_entry.entry_id)
await hass.async_block_till_done()
assert hass.data[DOMAIN][FEED].get(config_entry.entry_id) is None
@@ -0,0 +1,168 @@
"""The tests for the GeoNet NZ Volcano Feed integration."""
from asynctest import CoroutineMock, patch
from homeassistant.components import geonetnz_volcano
from homeassistant.components.geo_location import ATTR_DISTANCE
from homeassistant.components.geonetnz_volcano import DEFAULT_SCAN_INTERVAL
from homeassistant.components.geonetnz_volcano.const import (
ATTR_ACTIVITY,
ATTR_EXTERNAL_ID,
ATTR_HAZARDS,
)
from homeassistant.const import (
ATTR_ATTRIBUTION,
ATTR_FRIENDLY_NAME,
ATTR_ICON,
ATTR_LATITUDE,
ATTR_LONGITUDE,
ATTR_UNIT_OF_MEASUREMENT,
CONF_RADIUS,
EVENT_HOMEASSISTANT_START,
)
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from homeassistant.util.unit_system import IMPERIAL_SYSTEM
from tests.common import async_fire_time_changed
from tests.components.geonetnz_volcano import _generate_mock_feed_entry
CONFIG = {geonetnz_volcano.DOMAIN: {CONF_RADIUS: 200}}
async def test_setup(hass):
"""Test the general setup of the integration."""
# Set up some mock feed entries for this test.
mock_entry_1 = _generate_mock_feed_entry(
"1234",
"Title 1",
1,
15.5,
(38.0, -3.0),
attribution="Attribution 1",
activity="Activity 1",
hazards="Hazards 1",
)
mock_entry_2 = _generate_mock_feed_entry("2345", "Title 2", 0, 20.5, (38.1, -3.1))
mock_entry_3 = _generate_mock_feed_entry("3456", "Title 3", 2, 25.5, (38.2, -3.2))
mock_entry_4 = _generate_mock_feed_entry("4567", "Title 4", 1, 12.5, (38.3, -3.3))
# Patching 'utcnow' to gain more control over the timed update.
utcnow = dt_util.utcnow()
with patch("homeassistant.util.dt.utcnow", return_value=utcnow), patch(
"aio_geojson_client.feed.GeoJsonFeed.update", new_callable=CoroutineMock
) as mock_feed_update:
mock_feed_update.return_value = "OK", [mock_entry_1, mock_entry_2, mock_entry_3]
assert await async_setup_component(hass, geonetnz_volcano.DOMAIN, CONFIG)
# Artificially trigger update and collect events.
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
all_states = hass.states.async_all()
# 3 sensor entities
assert len(all_states) == 3
state = hass.states.get("sensor.volcano_title_1")
assert state is not None
assert state.name == "Volcano Title 1"
assert int(state.state) == 1
assert state.attributes[ATTR_EXTERNAL_ID] == "1234"
assert state.attributes[ATTR_LATITUDE] == 38.0
assert state.attributes[ATTR_LONGITUDE] == -3.0
assert state.attributes[ATTR_DISTANCE] == 15.5
assert state.attributes[ATTR_FRIENDLY_NAME] == "Volcano Title 1"
assert state.attributes[ATTR_ATTRIBUTION] == "Attribution 1"
assert state.attributes[ATTR_ACTIVITY] == "Activity 1"
assert state.attributes[ATTR_HAZARDS] == "Hazards 1"
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "alert level"
assert state.attributes[ATTR_ICON] == "mdi:image-filter-hdr"
state = hass.states.get("sensor.volcano_title_2")
assert state is not None
assert state.name == "Volcano Title 2"
assert int(state.state) == 0
assert state.attributes[ATTR_EXTERNAL_ID] == "2345"
assert state.attributes[ATTR_LATITUDE] == 38.1
assert state.attributes[ATTR_LONGITUDE] == -3.1
assert state.attributes[ATTR_DISTANCE] == 20.5
assert state.attributes[ATTR_FRIENDLY_NAME] == "Volcano Title 2"
state = hass.states.get("sensor.volcano_title_3")
assert state is not None
assert state.name == "Volcano Title 3"
assert int(state.state) == 2
assert state.attributes[ATTR_EXTERNAL_ID] == "3456"
assert state.attributes[ATTR_LATITUDE] == 38.2
assert state.attributes[ATTR_LONGITUDE] == -3.2
assert state.attributes[ATTR_DISTANCE] == 25.5
assert state.attributes[ATTR_FRIENDLY_NAME] == "Volcano Title 3"
# Simulate an update - two existing, one new entry, one outdated entry
mock_feed_update.return_value = "OK", [mock_entry_1, mock_entry_4, mock_entry_3]
async_fire_time_changed(hass, utcnow + DEFAULT_SCAN_INTERVAL)
await hass.async_block_till_done()
all_states = hass.states.async_all()
assert len(all_states) == 4
# Simulate an update - empty data, but successful update,
# so no changes to entities.
mock_feed_update.return_value = "OK_NO_DATA", None
async_fire_time_changed(hass, utcnow + 2 * DEFAULT_SCAN_INTERVAL)
await hass.async_block_till_done()
all_states = hass.states.async_all()
assert len(all_states) == 4
# Simulate an update - empty data, keep all entities
mock_feed_update.return_value = "ERROR", None
async_fire_time_changed(hass, utcnow + 3 * DEFAULT_SCAN_INTERVAL)
await hass.async_block_till_done()
all_states = hass.states.async_all()
assert len(all_states) == 4
# Simulate an update - regular data for 3 entries
mock_feed_update.return_value = "OK", [mock_entry_1, mock_entry_2, mock_entry_3]
async_fire_time_changed(hass, utcnow + 4 * DEFAULT_SCAN_INTERVAL)
await hass.async_block_till_done()
all_states = hass.states.async_all()
assert len(all_states) == 4
async def test_setup_imperial(hass):
"""Test the setup of the integration using imperial unit system."""
hass.config.units = IMPERIAL_SYSTEM
# Set up some mock feed entries for this test.
mock_entry_1 = _generate_mock_feed_entry("1234", "Title 1", 1, 15.5, (38.0, -3.0))
# Patching 'utcnow' to gain more control over the timed update.
utcnow = dt_util.utcnow()
with patch("homeassistant.util.dt.utcnow", return_value=utcnow), patch(
"aio_geojson_client.feed.GeoJsonFeed.update", new_callable=CoroutineMock
) as mock_feed_update, patch(
"aio_geojson_client.feed.GeoJsonFeed.__init__"
) as mock_feed_init:
mock_feed_update.return_value = "OK", [mock_entry_1]
assert await async_setup_component(hass, geonetnz_volcano.DOMAIN, CONFIG)
# Artificially trigger update and collect events.
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
all_states = hass.states.async_all()
assert len(all_states) == 1
# Test conversion of 200 miles to kilometers.
assert mock_feed_init.call_args[1].get("filter_radius") == 321.8688
state = hass.states.get("sensor.volcano_title_1")
assert state is not None
assert state.name == "Volcano Title 1"
assert int(state.state) == 1
assert state.attributes[ATTR_EXTERNAL_ID] == "1234"
assert state.attributes[ATTR_LATITUDE] == 38.0
assert state.attributes[ATTR_LONGITUDE] == -3.0
assert state.attributes[ATTR_DISTANCE] == 9.6
assert state.attributes[ATTR_FRIENDLY_NAME] == "Volcano Title 1"
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "alert level"
assert state.attributes[ATTR_ICON] == "mdi:image-filter-hdr"