def test_locations(self, mock): """Test the Canary locations API.""" _setup_responses(mock) api = Api("user", "pass") locations = api.get_locations() self.assertEqual(2, len(locations)) for location in locations: if location.name == "Vacation Home": self.assertTrue(location.is_recording) self.assertFalse(location.is_private) self.assertTrue(location.is_celsius) self.assertEqual(2, len(location.customers)) self.assertEqual("away", location.mode.name) self.assertEqual("armed", location.current_mode.name) self.assertEqual(70001, location.location_id) elif location.name == "Home": self.assertFalse(location.is_recording) self.assertFalse(location.is_private) self.assertFalse(location.is_celsius) self.assertEqual(1, len(location.customers)) self.assertEqual("home", location.mode.name) self.assertEqual("standby", location.current_mode.name) self.assertEqual(70002, location.location_id)
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)
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)