GeoNet NZ Quakes Sensor (#26078)

* working version of status sensor

* changed unit of measurement

* align naming with feed source

* simplified sensor name

* fix potential issue during initialisation

* fixed tests

* changed icon to constant

* added tests for new sensor

* split tests for geolocation vs sensor

* fixed lint

* fixed pylint

* fixed test

* removed config entry id from attributes

* moved entity manager to component

* fix issue with multiple config entries overriding each other's data

* creating async tasks instead of awaiting each unloading

* moved manager to component

* correctly triggering update only when this component is loaded

* fixed tests after major code refactorings

* fixed pylint

* moved actual creation of new events to geolocation platform

* changed all timestamps to utc

* changed the way platforms are setup and manager is updated

* simplify assert statement

* changed the way waiting for unloading platforms
This commit is contained in:
Malte Franken authored and Martin Hjelmare committed 2019-09-03 17:16:13 +02:00
1 parent fa79ef1220
commit 13bb2ea35a
7 files changed
+474 -169

No files matched your search

@@ -1 +1,31 @@
"""Tests for the geonetnz_quakes component."""
from unittest.mock import MagicMock
def _generate_mock_feed_entry(
external_id,
title,
distance_to_home,
coordinates,
attribution=None,
depth=None,
magnitude=None,
mmi=None,
locality=None,
quality=None,
time=None,
):
"""Construct a mock feed entry for testing purposes."""
feed_entry = MagicMock()
feed_entry.external_id = external_id
feed_entry.title = title
feed_entry.distance_to_home = distance_to_home
feed_entry.coordinates = coordinates
feed_entry.attribution = attribution
feed_entry.depth = depth
feed_entry.magnitude = magnitude
feed_entry.mmi = mmi
feed_entry.locality = locality
feed_entry.quality = quality
feed_entry.time = time
return feed_entry
@@ -1,6 +1,5 @@
"""The tests for the GeoNet NZ Quakes Feed integration."""
import datetime
from unittest.mock import MagicMock
from asynctest import patch, CoroutineMock
@@ -30,39 +29,11 @@ from homeassistant.setup import async_setup_component
from homeassistant.util.unit_system import IMPERIAL_SYSTEM
from tests.common import async_fire_time_changed
import homeassistant.util.dt as dt_util
from tests.components.geonetnz_quakes import _generate_mock_feed_entry
CONFIG = {geonetnz_quakes.DOMAIN: {CONF_RADIUS: 200}}
def _generate_mock_feed_entry(
external_id,
title,
distance_to_home,
coordinates,
attribution=None,
depth=None,
magnitude=None,
mmi=None,
locality=None,
quality=None,
time=None,
):
"""Construct a mock feed entry for testing purposes."""
feed_entry = MagicMock()
feed_entry.external_id = external_id
feed_entry.title = title
feed_entry.distance_to_home = distance_to_home
feed_entry.coordinates = coordinates
feed_entry.attribution = attribution
feed_entry.depth = depth
feed_entry.magnitude = magnitude
feed_entry.mmi = mmi
feed_entry.locality = locality
feed_entry.quality = quality
feed_entry.time = time
return feed_entry
async def test_setup(hass):
"""Test the general setup of the integration."""
# Set up some mock feed entries for this test.
@@ -94,13 +65,13 @@ async def test_setup(hass):
) 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_quakes.DOMAIN, CONFIG)
# Artificially trigger update.
# Artificially trigger update and collect events.
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
# Collect events.
await hass.async_block_till_done()
all_states = hass.states.async_all()
assert len(all_states) == 3
# 3 geolocation and 1 sensor entities
assert len(all_states) == 4
state = hass.states.get("geo_location.title_1")
assert state is not None
@@ -155,14 +126,13 @@ async def test_setup(hass):
}
assert float(state.state) == 25.5
# Simulate an update - one existing, one new entry,
# one outdated entry
# 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) == 3
assert len(all_states) == 4
# Simulate an update - empty data, but successful update,
# so no changes to entities.
@@ -171,7 +141,7 @@ async def test_setup(hass):
await hass.async_block_till_done()
all_states = hass.states.async_all()
assert len(all_states) == 3
assert len(all_states) == 4
# Simulate an update - empty data, removes all entities
mock_feed_update.return_value = "ERROR", None
@@ -179,7 +149,7 @@ async def test_setup(hass):
await hass.async_block_till_done()
all_states = hass.states.async_all()
assert len(all_states) == 0
assert len(all_states) == 1
async def test_setup_imperial(hass):
@@ -193,17 +163,22 @@ async def test_setup_imperial(hass):
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__", new_callable=CoroutineMock
) as mock_feed_init:
"aio_geojson_client.feed.GeoJsonFeed.__init__",
new_callable=CoroutineMock,
create=True,
) as mock_feed_init, patch(
"aio_geojson_client.feed.GeoJsonFeed.last_timestamp",
new_callable=CoroutineMock,
create=True,
):
mock_feed_update.return_value = "OK", [mock_entry_1]
assert await async_setup_component(hass, geonetnz_quakes.DOMAIN, CONFIG)
# Artificially trigger update.
# Artificially trigger update and collect events.
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
# Collect events.
await hass.async_block_till_done()
all_states = hass.states.async_all()
assert len(all_states) == 1
assert len(all_states) == 2
# Test conversion of 200 miles to kilometers.
assert mock_feed_init.call_args[1].get("filter_radius") == 321.8688
@@ -0,0 +1,115 @@
"""The tests for the GeoNet NZ Quakes Feed integration."""
import datetime
from asynctest import patch, CoroutineMock
from homeassistant.components import geonetnz_quakes
from homeassistant.components.geonetnz_quakes import DEFAULT_SCAN_INTERVAL
from homeassistant.components.geonetnz_quakes.sensor import (
ATTR_STATUS,
ATTR_LAST_UPDATE,
ATTR_CREATED,
ATTR_UPDATED,
ATTR_REMOVED,
ATTR_LAST_UPDATE_SUCCESSFUL,
)
from homeassistant.const import (
EVENT_HOMEASSISTANT_START,
CONF_RADIUS,
ATTR_UNIT_OF_MEASUREMENT,
ATTR_ICON,
)
from homeassistant.setup import async_setup_component
from tests.common import async_fire_time_changed
import homeassistant.util.dt as dt_util
from tests.components.geonetnz_quakes import _generate_mock_feed_entry
CONFIG = {geonetnz_quakes.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",
15.5,
(38.0, -3.0),
locality="Locality 1",
attribution="Attribution 1",
time=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc),
magnitude=5.7,
mmi=5,
depth=10.5,
quality="best",
)
mock_entry_2 = _generate_mock_feed_entry(
"2345", "Title 2", 20.5, (38.1, -3.1), magnitude=4.6
)
mock_entry_3 = _generate_mock_feed_entry(
"3456", "Title 3", 25.5, (38.2, -3.2), locality="Locality 3"
)
mock_entry_4 = _generate_mock_feed_entry("4567", "Title 4", 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_quakes.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 geolocation and 1 sensor entities
assert len(all_states) == 4
state = hass.states.get("sensor.geonet_nz_quakes_32_87336_117_22743")
assert state is not None
assert int(state.state) == 3
assert state.name == "GeoNet NZ Quakes (32.87336, -117.22743)"
attributes = state.attributes
assert attributes[ATTR_STATUS] == "OK"
assert attributes[ATTR_CREATED] == 3
assert attributes[ATTR_LAST_UPDATE].tzinfo == dt_util.UTC
assert attributes[ATTR_LAST_UPDATE_SUCCESSFUL].tzinfo == dt_util.UTC
assert attributes[ATTR_LAST_UPDATE] == attributes[ATTR_LAST_UPDATE_SUCCESSFUL]
assert attributes[ATTR_UNIT_OF_MEASUREMENT] == "quakes"
assert attributes[ATTR_ICON] == "mdi:pulse"
# 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
state = hass.states.get("sensor.geonet_nz_quakes_32_87336_117_22743")
attributes = state.attributes
assert attributes[ATTR_CREATED] == 1
assert attributes[ATTR_UPDATED] == 2
assert attributes[ATTR_REMOVED] == 1
# 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, removes 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) == 1
state = hass.states.get("sensor.geonet_nz_quakes_32_87336_117_22743")
attributes = state.attributes
assert attributes[ATTR_REMOVED] == 3