コード例 #1
0
ファイル: geo_location.py プロジェクト: dadaloop82/core
class IgnSismologiaFeedEntityManager:
    """Feed Entity Manager for IGN Sismologia GeoRSS feed."""

    def __init__(
        self,
        hass,
        add_entities,
        scan_interval,
        coordinates,
        radius_in_km,
        minimum_magnitude,
    ):
        """Initialize the Feed Entity Manager."""

        self._hass = hass
        self._feed_manager = IgnSismologiaFeedManager(
            self._generate_entity,
            self._update_entity,
            self._remove_entity,
            coordinates,
            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 = IgnSismologiaLocationEvent(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, f"ign_sismologia_update_{external_id}")

    def _remove_entity(self, external_id):
        """Remove entity."""
        dispatcher_send(self._hass, f"ign_sismologia_delete_{external_id}")
コード例 #2
0
    def __init__(self, hass, add_entities, scan_interval, coordinates,
                 radius_in_km, minimum_magnitude):
        """Initialize the Feed Entity Manager."""
        from georss_ign_sismologia_client import IgnSismologiaFeedManager

        self._hass = hass
        self._feed_manager = IgnSismologiaFeedManager(
            self._generate_entity, self._update_entity, self._remove_entity,
            coordinates, filter_radius=radius_in_km,
            filter_minimum_magnitude=minimum_magnitude)
        self._add_entities = add_entities
        self._scan_interval = scan_interval
コード例 #3
0
class IgnSismologiaFeedEntityManager:
    """Feed Entity Manager for IGN Sismologia GeoRSS feed."""

    def __init__(self, hass, add_entities, scan_interval, coordinates,
                 radius_in_km, minimum_magnitude):
        """Initialize the Feed Entity Manager."""
        from georss_ign_sismologia_client import IgnSismologiaFeedManager

        self._hass = hass
        self._feed_manager = IgnSismologiaFeedManager(
            self._generate_entity, self._update_entity, self._remove_entity,
            coordinates, 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 = IgnSismologiaLocationEvent(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))
コード例 #4
0
    def test_feed_manager(self, mock_session, mock_request):
        """Test the feed manager."""
        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("ign_sismologia_feed.xml"))

        # 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 = IgnSismologiaFeedManager(_generate_entity,
                                                _update_entity, _remove_entity,
                                                HOME_COORDINATES)
        assert (repr(feed_manager) == "<IgnSismologiaFeedManager("
                "feed=<IgnSismologiaFeed(home="
                "(40.38, -3.72), "
                "url=http://www.ign.es/ign/"
                "RssTools/sismologia.xml, "
                "radius=None, magnitude=None)>)>")
        feed_manager.update()
        entries = feed_manager.feed_entries
        self.assertIsNotNone(entries)
        assert len(entries) == 3
        assert feed_manager.last_timestamp == datetime.datetime(
            2019, 3, 18, 19, 34, 55)
        assert len(generated_entity_external_ids) == 3
        assert len(updated_entity_external_ids) == 0
        assert len(removed_entity_external_ids) == 0
コード例 #5
0
    def __init__(
        self,
        opp,
        add_entities,
        scan_interval,
        coordinates,
        radius_in_km,
        minimum_magnitude,
    ):
        """Initialize the Feed Entity Manager."""

        self._opp = opp
        self._feed_manager = IgnSismologiaFeedManager(
            self._generate_entity,
            self._update_entity,
            self._remove_entity,
            coordinates,
            filter_radius=radius_in_km,
            filter_minimum_magnitude=minimum_magnitude,
        )
        self._add_entities = add_entities
        self._scan_interval = scan_interval
コード例 #6
0
ファイル: geo_location.py プロジェクト: jbouwh/core
    def __init__(
        self,
        hass: HomeAssistant,
        add_entities: AddEntitiesCallback,
        scan_interval: timedelta,
        coordinates: tuple[float, float],
        radius_in_km: float,
        minimum_magnitude: float,
    ) -> None:
        """Initialize the Feed Entity Manager."""

        self._hass = hass
        self._feed_manager = IgnSismologiaFeedManager(
            self._generate_entity,
            self._update_entity,
            self._remove_entity,
            coordinates,
            filter_radius=radius_in_km,
            filter_minimum_magnitude=minimum_magnitude,
        )
        self._add_entities = add_entities
        self._scan_interval = scan_interval