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)
def test_location_with_motion_entry(self, mock): """Test the Canary entries API.""" _setup_responses(mock) api = Api("user", "pass") entries = api.get_entries(70001) self.assertEqual(1, len(entries)) entry = entries[0] self.assertEqual(60001, entry.entry_id) self.assertEqual("Activity detected in away mode", entry.description) self.assertEqual("motion", entry.entry_type) self.assertEqual("2017-11-19T06:50:44", entry.start_time) self.assertEqual("2017-11-19T07:00:44", entry.end_time) self.assertEqual(1, len(entry.thumbnails)) thumbnail = entry.thumbnails[0] self.assertEqual("https://image_url.com", thumbnail.image_url)