def __init__( self, hass, add_entities, scan_interval, coordinates, feed_type, radius_in_km, minimum_magnitude, ): """Initialize the Feed Entity Manager.""" from geojson_client.usgs_earthquake_hazards_program_feed import ( UsgsEarthquakeHazardsProgramFeedManager, ) self._hass = hass self._feed_manager = UsgsEarthquakeHazardsProgramFeedManager( self._generate_entity, self._update_entity, self._remove_entity, coordinates, feed_type, filter_radius=radius_in_km, filter_minimum_magnitude=minimum_magnitude, ) self._add_entities = add_entities self._scan_interval = scan_interval
class UsgsEarthquakesFeedEntityManager: """Feed Entity Manager for USGS Earthquake Hazards Program feed.""" def __init__( self, hass, add_entities, scan_interval, coordinates, feed_type, radius_in_km, minimum_magnitude, ): """Initialize the Feed Entity Manager.""" from geojson_client.usgs_earthquake_hazards_program_feed import ( UsgsEarthquakeHazardsProgramFeedManager, ) self._hass = hass self._feed_manager = UsgsEarthquakeHazardsProgramFeedManager( self._generate_entity, self._update_entity, self._remove_entity, coordinates, feed_type, filter_radius=radius_in_km, filter_minimum_magnitude=minimum_magnitude, ) self._add_entities = add_entities self._scan_interval = scan_interval def startup(self): """Start up this manager.""" self._feed_manager.update() self._init_regular_updates() def _init_regular_updates(self): """Schedule regular updates at the specified interval.""" track_time_interval(self._hass, lambda now: self._feed_manager.update(), self._scan_interval) def get_entry(self, external_id): """Get feed entry by external id.""" return self._feed_manager.feed_entries.get(external_id) def _generate_entity(self, external_id): """Generate new entity.""" new_entity = UsgsEarthquakesEvent(self, external_id) # Add new entities to HA. self._add_entities([new_entity], True) def _update_entity(self, external_id): """Update entity.""" dispatcher_send(self._hass, SIGNAL_UPDATE_ENTITY.format(external_id)) def _remove_entity(self, external_id): """Remove entity.""" dispatcher_send(self._hass, SIGNAL_DELETE_ENTITY.format(external_id))
class UsgsEarthquakesFeedEntityManager: """Feed Entity Manager for USGS Earthquake Hazards Program feed.""" def __init__(self, hass, add_entities, scan_interval, coordinates, feed_type, radius_in_km, minimum_magnitude): """Initialize the Feed Entity Manager.""" from geojson_client.usgs_earthquake_hazards_program_feed \ import UsgsEarthquakeHazardsProgramFeedManager self._hass = hass self._feed_manager = UsgsEarthquakeHazardsProgramFeedManager( self._generate_entity, self._update_entity, self._remove_entity, coordinates, feed_type, filter_radius=radius_in_km, filter_minimum_magnitude=minimum_magnitude) self._add_entities = add_entities self._scan_interval = scan_interval def startup(self): """Start up this manager.""" self._feed_manager.update() self._init_regular_updates() def _init_regular_updates(self): """Schedule regular updates at the specified interval.""" track_time_interval( self._hass, lambda now: self._feed_manager.update(), self._scan_interval) def get_entry(self, external_id): """Get feed entry by external id.""" return self._feed_manager.feed_entries.get(external_id) def _generate_entity(self, external_id): """Generate new entity.""" new_entity = UsgsEarthquakesEvent(self, external_id) # Add new entities to HA. self._add_entities([new_entity], True) def _update_entity(self, external_id): """Update entity.""" dispatcher_send(self._hass, SIGNAL_UPDATE_ENTITY.format(external_id)) def _remove_entity(self, external_id): """Remove entity.""" dispatcher_send(self._hass, SIGNAL_DELETE_ENTITY.format(external_id))
def test_feed_manager(self, mock_session, mock_request): """Test the feed manager.""" home_coordinates = (-31.0, 151.0) mock_session.return_value.__enter__.return_value.send\ .return_value.ok = True mock_session.return_value.__enter__.return_value.send\ .return_value.text = load_fixture( 'usgs_earthquake_hazards_program_feed.json') # This will just record calls and keep track of external ids. generated_entity_external_ids = [] updated_entity_external_ids = [] removed_entity_external_ids = [] def _generate_entity(external_id): """Generate new entity.""" generated_entity_external_ids.append(external_id) def _update_entity(external_id): """Update entity.""" updated_entity_external_ids.append(external_id) def _remove_entity(external_id): """Remove entity.""" removed_entity_external_ids.append(external_id) feed_manager = UsgsEarthquakeHazardsProgramFeedManager( _generate_entity, _update_entity, _remove_entity, home_coordinates, 'past_hour_significant_earthquakes') assert repr(feed_manager) == "<UsgsEarthquakeHazardsProgramFeed" \ "Manager(feed=<UsgsEarthquakeHazards" \ "ProgramFeed(home=(-31.0, 151.0), " \ "url=https://earthquake.usgs.gov/" \ "earthquakes/feed/v1.0/summary/" \ "significant_hour.geojson, " \ "radius=None, magnitude=None)>)>" feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 3 assert len(generated_entity_external_ids) == 3 assert len(updated_entity_external_ids) == 0 assert len(removed_entity_external_ids) == 0
def __init__(self, hass, add_entities, scan_interval, coordinates, feed_type, radius_in_km, minimum_magnitude): """Initialize the Feed Entity Manager.""" from geojson_client.usgs_earthquake_hazards_program_feed \ import UsgsEarthquakeHazardsProgramFeedManager self._hass = hass self._feed_manager = UsgsEarthquakeHazardsProgramFeedManager( self._generate_entity, self._update_entity, self._remove_entity, coordinates, feed_type, filter_radius=radius_in_km, filter_minimum_magnitude=minimum_magnitude) self._add_entities = add_entities self._scan_interval = scan_interval
def __init__( self, opp, add_entities, scan_interval, coordinates, feed_type, radius_in_km, minimum_magnitude, ): """Initialize the Feed Entity Manager.""" self._opp = opp self._feed_manager = UsgsEarthquakeHazardsProgramFeedManager( self._generate_entity, self._update_entity, self._remove_entity, coordinates, feed_type, filter_radius=radius_in_km, filter_minimum_magnitude=minimum_magnitude, ) self._add_entities = add_entities self._scan_interval = scan_interval
def test_feed_manager(self, mock_session, mock_request): """Test the feed manager.""" home_coordinates = (-31.0, 151.0) mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("usgs_earthquake_hazards_program_feed.json")) # This will just record calls and keep track of external ids. generated_entity_external_ids = [] updated_entity_external_ids = [] removed_entity_external_ids = [] def _generate_entity(external_id): """Generate new entity.""" generated_entity_external_ids.append(external_id) def _update_entity(external_id): """Update entity.""" updated_entity_external_ids.append(external_id) def _remove_entity(external_id): """Remove entity.""" removed_entity_external_ids.append(external_id) feed_manager = UsgsEarthquakeHazardsProgramFeedManager( _generate_entity, _update_entity, _remove_entity, home_coordinates, "past_hour_significant_earthquakes", ) assert (repr(feed_manager) == "<UsgsEarthquakeHazardsProgramFeed" "Manager(feed=<UsgsEarthquakeHazards" "ProgramFeed(home=(-31.0, 151.0), " "url=https://earthquake.usgs.gov/" "earthquakes/feed/v1.0/summary/" "significant_hour.geojson, " "radius=None, magnitude=None)>)>") feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 3 assert len(generated_entity_external_ids) == 3 assert len(updated_entity_external_ids) == 0 assert len(removed_entity_external_ids) == 0 # Simulate an update with overridden filter. generated_entity_external_ids.clear() updated_entity_external_ids.clear() removed_entity_external_ids.clear() feed_manager.update_override({"minimum_magnitude": 2.5}) entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 1 assert len(generated_entity_external_ids) == 0 assert len(updated_entity_external_ids) == 1 assert len(removed_entity_external_ids) == 2 # Simulate an update with two overridden filters. generated_entity_external_ids.clear() updated_entity_external_ids.clear() removed_entity_external_ids.clear() feed_manager.update_override({ "minimum_magnitude": 1.0, "radius": 1000.0 }) entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 2 assert len(generated_entity_external_ids) == 1 assert len(updated_entity_external_ids) == 1 assert len(removed_entity_external_ids) == 0