Ejemplo n.º 1
0
 def __init__(self, home_coordinates, url, radius, category, service_name,
              unit_of_measurement):
     """Initialize the sensor."""
     self._category = category
     self._service_name = service_name
     self._state = STATE_UNKNOWN
     self._state_attributes = None
     self._unit_of_measurement = unit_of_measurement
     from georss_client.generic_feed import GenericFeed
     self._feed = GenericFeed(home_coordinates, url, filter_radius=radius,
                              filter_categories=None if not category
                              else [category])
Ejemplo n.º 2
0
    def __init__(self, coordinates, url, radius, category, service_name,
                 unit_of_measurement):
        """Initialize the sensor."""
        self._category = category
        self._service_name = service_name
        self._state = None
        self._state_attributes = None
        self._unit_of_measurement = unit_of_measurement

        self._feed = GenericFeed(
            coordinates,
            url,
            filter_radius=radius,
            filter_categories=None if not category else [category],
        )
Ejemplo n.º 3
0
class GeoRssServiceSensor(Entity):
    """Representation of a Sensor."""

    def __init__(self, home_coordinates, url, radius, category, service_name,
                 unit_of_measurement):
        """Initialize the sensor."""
        self._category = category
        self._service_name = service_name
        self._state = STATE_UNKNOWN
        self._state_attributes = None
        self._unit_of_measurement = unit_of_measurement
        from georss_client.generic_feed import GenericFeed
        self._feed = GenericFeed(home_coordinates, url, filter_radius=radius,
                                 filter_categories=None if not category
                                 else [category])

    @property
    def name(self):
        """Return the name of the sensor."""
        return '{} {}'.format(self._service_name,
                              'Any' if self._category is None
                              else self._category)

    @property
    def state(self):
        """Return the state of the sensor."""
        return self._state

    @property
    def unit_of_measurement(self):
        """Return the unit of measurement."""
        return self._unit_of_measurement

    @property
    def icon(self):
        """Return the default icon to use in the frontend."""
        return DEFAULT_ICON

    @property
    def device_state_attributes(self):
        """Return the state attributes."""
        return self._state_attributes

    def update(self):
        """Update this sensor from the GeoRSS service."""
        import georss_client
        status, feed_entries = self._feed.update()
        if status == georss_client.UPDATE_OK:
            _LOGGER.debug("Adding events to sensor %s: %s", self.entity_id,
                          feed_entries)
            self._state = len(feed_entries)
            # And now compute the attributes from the filtered events.
            matrix = {}
            for entry in feed_entries:
                matrix[entry.title] = '{:.0f}km'.format(
                    entry.distance_to_home)
            self._state_attributes = matrix
        elif status == georss_client.UPDATE_OK_NO_DATA:
            _LOGGER.debug("Update successful, but no data received from %s",
                          self._feed)
            # Don't change the state or state attributes.
        else:
            _LOGGER.warning("Update not successful, no data received from %s",
                            self._feed)
            # If no events were found due to an error then just set state to
            # zero.
            self._state = 0