Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ jobs:
strategy:
fail-fast: true
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "pypy-3.9", "pypy-3.10"]
python-version: ["3.10", "3.11", "3.12", "3.13", "pypy-3.10"]
os: [ubuntu-latest, macos-latest, windows-latest]
exclude:
# Exclude pypy on windows due to performance issues
- os: windows-latest
python-version: "pypy-3.9"
- os: windows-latest
python-version: "pypy-3.10"

Expand Down
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ description = "A Python library to communicate with Ring Door Bell (https://ring
authors = [{ name = "python-ring-doorbell developers" }]
license = { text="LGPL-3.0-or-later" }
readme = "README.rst"
requires-python = ">=3.9.0"
requires-python = ">=3.10.0"
dependencies = [
"oauthlib>=3.0.0,<4",
"pytz>=2022.0",
"asyncclick>=8.1.7.1",
"aiohttp>=3",
"aiohttp>=3.14",
"aiofiles>=23",
"typing-extensions>=4.12.2,<5.0",
"async-timeout>=3.0.0",
Expand All @@ -30,7 +30,6 @@ classifiers = [
"License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
Expand Down Expand Up @@ -75,7 +74,7 @@ dev-dependencies = [
"types-pytz>=2022.0",
"pytest-freezer~=0.4",
"types-oauthlib>=3.0.0,<4",
"aioresponses~=0.7",
"aiointercept>=0.1.9",
"types-aiofiles>=23",
"mypy~=1.0"
]
Expand All @@ -87,7 +86,8 @@ testpaths = [
norecursedirs = ".git"
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
addopts = "--disable-socket --allow-unix-socket"
# aiointercept serves the mocked responses from a real server on localhost.
addopts = "--disable-socket --allow-unix-socket --allow-hosts=127.0.0.1,::1"
filterwarnings = [
"ignore:.*google._upb._message.MessageMapContainer uses PyType_Spec.*:DeprecationWarning",
"ignore:.*google._upb._message.ScalarMapContainer uses PyType_Spec.*:DeprecationWarning",
Expand Down
14 changes: 9 additions & 5 deletions ring_doorbell/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from json import loads as json_loads
from typing import TYPE_CHECKING, Any, Callable, ClassVar

from aiohttp import BasicAuth, ClientError, ClientResponseError, ClientSession
from aiohttp import ClientError, ClientResponseError, ClientSession, encode_basic_auth
from oauthlib.common import urldecode
from oauthlib.oauth2 import (
LegacyApplicationClient,
Expand Down Expand Up @@ -65,7 +65,7 @@ def __init__(
self._oauth_client = LegacyApplicationClient(
client_id=OAuth.CLIENT_ID, token=token
)
self._auth = BasicAuth(OAuth.CLIENT_ID, "")
self._auth_header = encode_basic_auth(OAuth.CLIENT_ID)

@property
def _session(self) -> ClientSession:
Expand All @@ -84,7 +84,11 @@ async def async_fetch_token(
:type password: str
:type otp_code: str.
"""
headers = {"User-Agent": self.user_agent, "hardware_id": self.hardware_id}
headers = {
"User-Agent": self.user_agent,
"hardware_id": self.hardware_id,
"Authorization": self._auth_header,
}

if otp_code:
headers["2fa-support"] = "true"
Expand All @@ -100,7 +104,6 @@ async def async_fetch_token(
OAuth.ENDPOINT,
data=data,
headers=headers,
auth=self._auth,
)
async with resp:
text = await resp.text()
Expand All @@ -123,13 +126,14 @@ async def async_refresh_tokens(self) -> dict[str, Any]:
headers = {
"Accept": "application/json",
"Content-Type": ("application/x-www-form-urlencoded;charset=UTF-8"),
"Authorization": self._auth_header,
}
body = self._oauth_client.prepare_refresh_body(
refresh_token=self._token["refresh_token"]
)
data = dict(urldecode(body))
resp = await self._session.request(
"POST", OAuth.ENDPOINT, data=data, headers=headers, auth=self._auth
"POST", OAuth.ENDPOINT, data=data, headers=headers
)
async with resp:
text = await resp.text()
Expand Down
13 changes: 5 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from unittest.mock import patch

import pytest
from aioresponses import CallbackResult, aioresponses
from aiointercept import CallbackResult, aiointercept
from ring_doorbell import Auth, Ring
from ring_doorbell.const import USER_AGENT

Expand All @@ -28,7 +28,6 @@ def json_request_kwargs():
"Content-Type": "application/json",
"Authorization": "Bearer dummyBearerToken",
},
"timeout": 10,
"data": None,
"params": {},
"json": {},
Expand All @@ -42,7 +41,6 @@ def nojson_request_kwargs():
"hardware_id": "21ac3af1-0eac-5fbd-8b0f-0b784889bfbd",
"Authorization": "Bearer dummyBearerToken",
},
"timeout": 10,
"data": None,
"params": {},
}
Expand Down Expand Up @@ -197,11 +195,10 @@ def _hardware_id_mock_fixture() -> Generator:
yield


# setting the fixture name to requests_mock allows other
# tests to pull in request_mock and append uris
@pytest.fixture(autouse=True, name="aioresponses_mock")
def aioresponses_mock_fixture(request, devices_fixture, putpatch_status_fixture):
with aioresponses() as mock:
# setting the fixture name allows other tests to pull in the mock and append uris
@pytest.fixture(autouse=True, name="aiointercept_mock")
async def aiointercept_mock_fixture(request, devices_fixture, putpatch_status_fixture):
async with aiointercept(mock_external_urls=True) as mock:
mock.post(
"https://oauth.ring.com/oauth/token",
payload=load_fixture_as_dict("ring_oauth.json"),
Expand Down
17 changes: 7 additions & 10 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from tests.conftest import (
load_alert_v1,
load_fixture,
load_fixture_as_dict,
)


Expand Down Expand Up @@ -168,7 +167,7 @@ def _add_token(
assert res.exit_code == 0


async def test_motion_detection(ring, aioresponses_mock, devices_fixture):
async def test_motion_detection(ring, aiointercept_mock, devices_fixture):
runner = CliRunner()
with runner.isolated_filesystem():
res = await runner.invoke(
Expand All @@ -189,12 +188,10 @@ async def test_motion_detection(ring, aioresponses_mock, devices_fixture):
assert res.exit_code == 0
assert expected in res.output

# Changes the return to indicate that the siren is now on.
# Changes the return to indicate that the siren is now on; the
# devices handler is registered with repeat=True and serves the
# updated fixture from here on.
devices_fixture.updated = True
aioresponses_mock.get(
"https://api.ring.com/clients_api/ring_devices",
payload=load_fixture_as_dict("ring_devices_updated.json"),
)

res = await runner.invoke(
motion_detection,
Expand Down Expand Up @@ -269,7 +266,7 @@ async def test_listen_event_handler(mocker, auth):
echomock.assert_called_with(exp)


async def test_in_home_chime(ring, aioresponses_mock, devices_fixture):
async def test_in_home_chime(ring, aiointercept_mock, devices_fixture):
runner = CliRunner()
with runner.isolated_filesystem():
# Gets in-home chime details for a doorbell
Expand Down Expand Up @@ -345,7 +342,7 @@ async def test_in_home_chime(ring, aioresponses_mock, devices_fixture):
assert expected in res.output


async def test_open_door(ring, aioresponses_mock, devices_fixture):
async def test_open_door(ring, aiointercept_mock, devices_fixture):
runner = CliRunner()

res = await runner.invoke(
Expand All @@ -357,7 +354,7 @@ async def test_open_door(ring, aioresponses_mock, devices_fixture):
assert res.output == "Ingress opened\n"


async def test_get_device(ring, aioresponses_mock, devices_fixture):
async def test_get_device(ring, aiointercept_mock, devices_fixture):
runner = CliRunner()

# Get device by name
Expand Down
22 changes: 11 additions & 11 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,54 +45,54 @@ async def test_other_attributes(ring):
assert dev.wifi_signal_strength != 100


async def test_other_controls(ring, aioresponses_mock):
async def test_other_controls(ring, aiointercept_mock):
dev = ring.devices()["other"][0]

kwargs = json_request_kwargs()
kwargs["json"] = None

await dev.async_set_doorbell_volume(6)
kwargs["params"] = {"doorbot[settings][doorbell_volume]": "6"}
aioresponses_mock.assert_called_with(
aiointercept_mock.assert_called_with(
"https://api.ring.com/clients_api/doorbots/185036587", method="PUT", **kwargs
)

kwargs = json_request_kwargs()

await dev.async_set_mic_volume(10)
kwargs["json"] = {"volume_settings": {"mic_volume": 10}}
aioresponses_mock.assert_called_with(
aiointercept_mock.assert_called_with(
"https://api.ring.com/devices/v1/devices/185036587/settings",
method="PATCH",
**kwargs,
)

await dev.async_set_voice_volume(9)
kwargs["json"] = {"volume_settings": {"voice_volume": 9}}
aioresponses_mock.assert_called_with(
aiointercept_mock.assert_called_with(
"https://api.ring.com/devices/v1/devices/185036587/settings",
method="PATCH",
**kwargs,
)

await dev.async_set_clip_length_max(30)
kwargs["json"] = {"video_settings": {"clip_length_max": 30}}
aioresponses_mock.assert_called_with(
aiointercept_mock.assert_called_with(
"https://api.ring.com/devices/v1/devices/185036587/settings",
method="PATCH",
**kwargs,
)

await dev.async_set_keep_alive_auto(32.2)
kwargs["json"] = {"keep_alive_settings": {"keep_alive_auto": 32.2}}
aioresponses_mock.assert_called_with(
aiointercept_mock.assert_called_with(
"https://api.ring.com/devices/v1/devices/185036587/settings",
method="PATCH",
**kwargs,
)


async def test_other_invitations(ring, aioresponses_mock):
async def test_other_invitations(ring, aiointercept_mock):
dev = ring.devices()["other"][0]
kwargs = json_request_kwargs()
kwargs["json"] = {
Expand All @@ -104,7 +104,7 @@ async def test_other_invitations(ring, aioresponses_mock):
}

await dev.async_invite_access("test@example.com")
aioresponses_mock.assert_called_with(
aiointercept_mock.assert_called_with(
"https://api.ring.com/clients_api/locations/mock-location-id/invitations",
method="POST",
**kwargs,
Expand All @@ -113,14 +113,14 @@ async def test_other_invitations(ring, aioresponses_mock):
await dev.async_remove_access(123456789)

kwargs = nojson_request_kwargs()
aioresponses_mock.assert_called_with(
aiointercept_mock.assert_called_with(
"https://api.ring.com/clients_api/locations/mock-location-id/invitations/123456789",
method="DELETE",
**kwargs,
)


async def test_other_open_door(ring, aioresponses_mock, mocker):
async def test_other_open_door(ring, aiointercept_mock, mocker):
dev = ring.devices()["other"][0]

mocker.patch("uuid.uuid4", return_value="987654321")
Expand All @@ -137,7 +137,7 @@ async def test_other_open_door(ring, aioresponses_mock, mocker):
}

await dev.async_open_door(15)
aioresponses_mock.assert_called_with(
aiointercept_mock.assert_called_with(
"https://api.ring.com/commands/v1/devices/185036587/device_rpc",
method="PUT",
**kwargs,
Expand Down
Loading
Loading