예제 #1
0
    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)
예제 #2
0
    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()
예제 #3
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)
예제 #4
0
    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()
예제 #5
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)
예제 #6
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)
예제 #7
0
def _get_canary_api_instance(entry: ConfigEntry) -> Api:
    """Initialize a new instance of CanaryApi."""
    canary = Api(
        entry.data[CONF_USERNAME],
        entry.data[CONF_PASSWORD],
        entry.options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT),
    )

    return canary
예제 #8
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)
예제 #9
0
    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)
예제 #10
0
    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()
예제 #11
0
def _get_canary_data_instance(entry: ConfigEntry) -> CanaryData:
    """Initialize a new instance of CanaryData."""
    canary = Api(
        entry.data[CONF_USERNAME],
        entry.data[CONF_PASSWORD],
        entry.options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT),
    )

    canary_data = CanaryData(canary)
    canary_data.update()

    return canary_data
예제 #12
0
def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
    """Validate the user input allows us to connect.

    Data has the keys from DATA_SCHEMA with values provided by the user.
    """
    # constructor does login call
    Api(
        data[CONF_USERNAME],
        data[CONF_PASSWORD],
        data.get(CONF_TIMEOUT, DEFAULT_TIMEOUT),
    )

    return True
예제 #13
0
def validate_input(opp: OpenPeerPower, data: ConfigType) -> bool:
    """Validate the user input allows us to connect.

    Data has the keys from DATA_SCHEMA with values provided by the user.
    """
    # constructor does login call
    Api(
        data[CONF_USERNAME],
        data[CONF_PASSWORD],
        data.get(CONF_TIMEOUT, DEFAULT_TIMEOUT),
    )

    return True
예제 #14
0
def canary_config_flow(hass):
    """Mock the CanaryApi for easier config flow testing."""
    with patch.object(Api, "login", return_value=True), patch(
            "homeassistant.components.canary.config_flow.Api") as mock_canary:
        instance = mock_canary.return_value = Api(
            "test-username",
            "test-password",
            1,
        )

        instance.login = MagicMock(return_value=True)
        instance.get_entries = MagicMock(return_value=[])
        instance.get_locations = MagicMock(return_value=[])
        instance.get_location = MagicMock(return_value=None)
        instance.get_modes = MagicMock(return_value=[])
        instance.get_readings = MagicMock(return_value=[])
        instance.get_latest_readings = MagicMock(return_value=[])
        instance.set_location_mode = MagicMock(return_value=None)

        yield mock_canary
예제 #15
0
def canary(opp):
    """Mock the CanaryApi for easier testing."""
    with patch.object(Api, "login", return_value=True), patch(
        "openpeerpower.components.canary.Api"
    ) as mock_canary:
        instance = mock_canary.return_value = Api(
            "test-username",
            "test-password",
            1,
        )

        instance.login = MagicMock(return_value=True)
        instance.get_entries = MagicMock(return_value=[])
        instance.get_locations = MagicMock(return_value=[])
        instance.get_location = MagicMock(return_value=None)
        instance.get_modes = MagicMock(return_value=[])
        instance.get_readings = MagicMock(return_value=[])
        instance.get_latest_readings = MagicMock(return_value=[])
        instance.set_location_mode = MagicMock(return_value=None)

        yield mock_canary