Exemplo n.º 1
0
class QldBushfireFeedEntityManager:
    """Feed Entity Manager for Qld Bushfire Alert GeoRSS feed."""
    def __init__(
        self,
        hass: HomeAssistant,
        add_entities: AddEntitiesCallback,
        scan_interval: timedelta,
        coordinates: tuple[float, float],
        radius_in_km: float,
        categories: list[str],
    ) -> None:
        """Initialize the Feed Entity Manager."""
        self._hass = hass
        self._feed_manager = QldBushfireAlertFeedManager(
            self._generate_entity,
            self._update_entity,
            self._remove_entity,
            coordinates,
            filter_radius=radius_in_km,
            filter_categories=categories,
        )
        self._add_entities = add_entities
        self._scan_interval = scan_interval

    def startup(self) -> None:
        """Start up this manager."""
        self._feed_manager.update()
        self._init_regular_updates()

    def _init_regular_updates(self) -> None:
        """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: str) -> QldBushfireAlertFeedEntry | None:
        """Get feed entry by external id."""
        return self._feed_manager.feed_entries.get(external_id)

    def _generate_entity(self, external_id: str) -> None:
        """Generate new entity."""
        new_entity = QldBushfireLocationEvent(self, external_id)
        # Add new entities to HA.
        self._add_entities([new_entity], True)

    def _update_entity(self, external_id: str) -> None:
        """Update entity."""
        dispatcher_send(self._hass, SIGNAL_UPDATE_ENTITY.format(external_id))

    def _remove_entity(self, external_id: str) -> None:
        """Remove entity."""
        dispatcher_send(self._hass, SIGNAL_DELETE_ENTITY.format(external_id))
Exemplo n.º 2
0
 def __init__(self, hass, add_entities, scan_interval, coordinates,
              radius_in_km, categories):
     """Initialize the Feed Entity Manager."""
     self._hass = hass
     self._feed_manager = QldBushfireAlertFeedManager(
         self._generate_entity,
         self._update_entity,
         self._remove_entity,
         coordinates,
         filter_radius=radius_in_km,
         filter_categories=categories)
     self._add_entities = add_entities
     self._scan_interval = scan_interval
Exemplo n.º 3
0
class QldBushfireFeedEntityManager:
    """Feed Entity Manager for Qld Bushfire Alert GeoRSS feed."""
    def __init__(self, opp, add_entities, scan_interval, coordinates,
                 radius_in_km, categories):
        """Initialize the Feed Entity Manager."""
        self._opp = opp
        self._feed_manager = QldBushfireAlertFeedManager(
            self._generate_entity,
            self._update_entity,
            self._remove_entity,
            coordinates,
            filter_radius=radius_in_km,
            filter_categories=categories,
        )
        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._opp, 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 = QldBushfireLocationEvent(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._opp, SIGNAL_UPDATE_ENTITY.format(external_id))

    def _remove_entity(self, external_id):
        """Remove entity."""
        dispatcher_send(self._opp, SIGNAL_DELETE_ENTITY.format(external_id))
    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("qld_bushfire_alert_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 = QldBushfireAlertFeedManager(
            _generate_entity, _update_entity, _remove_entity, HOME_COORDINATES
        )
        assert (
            repr(feed_manager) == "<QldBushfireAlertFeedManager("
            "feed=<QldBushfireAlertFeed(home="
            "(-31.0, 151.0), url=https://www."
            "qfes.qld.gov.au/data/alerts/"
            "bushfireAlert.xml, "
            "radius=None, categories=None)>)>"
        )
        feed_manager.update()
        entries = feed_manager.feed_entries
        self.assertIsNotNone(entries)
        assert len(entries) == 2
        assert feed_manager.last_timestamp == datetime.datetime(2018, 9, 27, 8, 0)
        assert len(generated_entity_external_ids) == 2
        assert len(updated_entity_external_ids) == 0
        assert len(removed_entity_external_ids) == 0
Exemplo n.º 5
0
 def __init__(
     self,
     hass: HomeAssistant,
     add_entities: AddEntitiesCallback,
     scan_interval: timedelta,
     coordinates: tuple[float, float],
     radius_in_km: float,
     categories: list[str],
 ) -> None:
     """Initialize the Feed Entity Manager."""
     self._hass = hass
     self._feed_manager = QldBushfireAlertFeedManager(
         self._generate_entity,
         self._update_entity,
         self._remove_entity,
         coordinates,
         filter_radius=radius_in_km,
         filter_categories=categories,
     )
     self._add_entities = add_entities
     self._scan_interval = scan_interval