Ejemplo n.º 1
0
class CanaryData:
    """Get the latest data and update the states."""
    def __init__(self, username, password, timeout):
        """Init the Canary data object."""

        self._api = Api(username, password, timeout)

        self._locations_by_id = {}
        self._readings_by_device_id = {}

        self.update()

    @Throttle(MIN_TIME_BETWEEN_UPDATES)
    def update(self, **kwargs):
        """Get the latest data from py-canary."""
        for location in self._api.get_locations():
            location_id = location.location_id

            self._locations_by_id[location_id] = location

            for device in location.devices:
                if device.is_online:
                    self._readings_by_device_id[
                        device.device_id] = self._api.get_latest_readings(
                            device.device_id)

    @property
    def locations(self):
        """Return a list of locations."""
        return self._locations_by_id.values()

    def get_location(self, location_id):
        """Return a location based on location_id."""
        return self._locations_by_id.get(location_id, [])

    def get_readings(self, device_id):
        """Return a list of readings based on device_id."""
        return self._readings_by_device_id.get(device_id, [])

    def get_reading(self, device_id, sensor_type):
        """Return reading for device_id and sensor type."""
        readings = self._readings_by_device_id.get(device_id, [])
        return next(
            (reading.value
             for reading in readings if reading.sensor_type == sensor_type),
            None,
        )

    def set_location_mode(self, location_id, mode_name, is_private=False):
        """Set location mode."""
        self._api.set_location_mode(location_id, mode_name, is_private)
        self.update(no_throttle=True)

    def get_live_stream_session(self, device):
        """Return live stream session."""
        return self._api.get_live_stream_session(device)
Ejemplo n.º 2
0
class CanaryData(object):
    """Get the latest data and update the states."""

    def __init__(self, username, password, timeout):
        """Init the Canary data object."""
        from canary.api import Api
        self._api = Api(username, password, timeout)

        self._locations_by_id = {}
        self._readings_by_device_id = {}
        self._entries_by_location_id = {}

        self.update()

    @Throttle(MIN_TIME_BETWEEN_UPDATES)
    def update(self, **kwargs):
        """Get the latest data from py-canary."""
        for location in self._api.get_locations():
            location_id = location.location_id

            self._locations_by_id[location_id] = location
            self._entries_by_location_id[location_id] = self._api.get_entries(
                location_id, entry_type="motion", limit=1)

            for device in location.devices:
                if device.is_online:
                    self._readings_by_device_id[device.device_id] = \
                        self._api.get_latest_readings(device.device_id)

    @property
    def locations(self):
        """Return a list of locations."""
        return self._locations_by_id.values()

    def get_motion_entries(self, location_id):
        """Return a list of motion entries based on location_id."""
        return self._entries_by_location_id.get(location_id, [])

    def get_location(self, location_id):
        """Return a location based on location_id."""
        return self._locations_by_id.get(location_id, [])

    def get_readings(self, device_id):
        """Return a list of readings based on device_id."""
        return self._readings_by_device_id.get(device_id, [])

    def set_location_mode(self, location_id, mode_name, is_private=False):
        """Set location mode."""
        self._api.set_location_mode(location_id, mode_name, is_private)
        self.update(no_throttle=True)
Ejemplo n.º 3
0
class CanaryData(object):
    """Get the latest data and update the states."""
    def __init__(self, username, password, timeout):
        """Init the Canary data object."""
        from canary.api import Api
        self._api = Api(username, password, timeout)

        self._locations_by_id = {}
        self._readings_by_device_id = {}
        self._entries_by_location_id = {}

        self.update()

    @Throttle(MIN_TIME_BETWEEN_UPDATES)
    def update(self, **kwargs):
        """Get the latest data from py-canary."""
        for location in self._api.get_locations():
            location_id = location.location_id

            self._locations_by_id[location_id] = location
            self._entries_by_location_id[location_id] = self._api.get_entries(
                location_id, entry_type="motion", limit=1)

            for device in location.devices:
                if device.is_online:
                    self._readings_by_device_id[device.device_id] = \
                        self._api.get_latest_readings(device.device_id)

    @property
    def locations(self):
        """Return a list of locations."""
        return self._locations_by_id.values()

    def get_motion_entries(self, location_id):
        """Return a list of motion entries based on location_id."""
        return self._entries_by_location_id.get(location_id, [])

    def get_location(self, location_id):
        """Return a location based on location_id."""
        return self._locations_by_id.get(location_id, [])

    def get_readings(self, device_id):
        """Return a list of readings based on device_id."""
        return self._readings_by_device_id.get(device_id, [])

    def set_location_mode(self, location_id, mode_name, is_private=False):
        """Set location mode."""
        self._api.set_location_mode(location_id, mode_name, is_private)
        self.update(no_throttle=True)
Ejemplo n.º 4
0
    def test_device_with_readings(self, mock):
        """Test the Canary entries API."""
        _setup_responses(mock)
        api = Api("user", "pass")

        readings = api.get_readings(80001)
        self.assertEqual(6, len(readings))

        readings = api.get_latest_readings(80001)
        self.assertEqual(3, len(readings))

        for reading in readings:
            if reading.sensor_type == SensorType.AIR_QUALITY:
                self.assertEqual("0.8129177689552307", reading.value)
            elif reading.sensor_type == SensorType.HUMIDITY:
                self.assertEqual("41.68813060192352", reading.value)
            elif reading.sensor_type == SensorType.TEMPERATURE:
                self.assertEqual("19.0007521446715", reading.value)