Skip to content

Commit

Permalink
Replaced dnspython with aiodns to fix home-assistant/core#119979
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveEasley committed Aug 9, 2024
1 parent 2d3c082 commit 9522254
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion jvcprojector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
)
from .projector import JvcProjector

__version__ = "1.0.11"
__version__ = "1.0.12"
36 changes: 11 additions & 25 deletions jvcprojector/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
from __future__ import annotations

import asyncio
import re
import socket

import dns.asyncresolver
import dns.exception
import dns.resolver
from dns.rdtypes.IN.A import A
import aiodns

from .error import JvcProjectorConnectError

Expand Down Expand Up @@ -68,24 +65,13 @@ async def disconnect(self) -> None:
self._reader = None


async def resolve(host: str, timeout: int = 5) -> str:
async def resolve(host: str) -> str:
"""Resolve hostname to ip address."""

async def _resolve() -> str:
resolver = dns.asyncresolver.Resolver()
answer: list[A] = await resolver.resolve(host, rdtype="A", lifetime=timeout)
if len(answer) == 0:
raise JvcProjectorConnectError(f"DNS failure resolving host {host}")
return answer[0].to_text()

ip_address = host

if re.search("^[0-9.]+$", host) is None:
try:
ip_address = await _resolve()
except dns.exception.DNSException as err:
raise JvcProjectorConnectError(f"Failed to resolve host {host}") from err
else:
ip_address = re.sub(r"\b0+(\d)", r"\1", ip_address)

return ip_address
try:
res = await aiodns.DNSResolver().gethostbyname(host, socket.AF_INET)
if len(res.addresses) < 1:
raise aiodns.error.DNSError("Unexpected zero length addresses response")
except aiodns.error.DNSError as err:
raise JvcProjectorConnectError(f"Failed to resolve host {host}") from err

return res.addresses[0]
2 changes: 1 addition & 1 deletion jvcprojector/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
REMOTE_3D_FORMAT: Final = "73D6"
REMOTE_PIC_ADJ: Final = "7372"
REMOTE_NATURAL: Final = "736A"
REMOTE_CINEMA: Final = "7368"
REMOTE_CINEMA: Final = "7368"
4 changes: 3 additions & 1 deletion jvcprojector/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ async def send(self, cmds: list[JvcCommand]) -> None:
if not self._conn.is_connected():
await self._connect()

cmd = None

for cmd in cmds:
await self._send(cmd)
# Throttle commands since some projectors dont like back to back commands
# Throttle since some projectors dont like back to back commands
await asyncio.sleep(0.5)
# If device is not powered on, skip remaining commands
if is_refresh and cmds[0].response != const.ON:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dnspython>=2.2.1
aiodns>=3.2.0

2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ classifiers =
[options]
packages = jvcprojector
install_requires =
dnspython >= 2.2.1
aiodns >= 3.2.0

[options.package_data]
jvcprojector = py.typed
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from jvcprojector.device import END

IP = "127.0.0.1"
HOST = "localhost"
PORT = 12345
TIMEOUT = 3.0
MAC = "abcd1234"
Expand Down
12 changes: 11 additions & 1 deletion tests/test_projector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from jvcprojector.error import JvcProjectorError
from jvcprojector.projector import JvcProjector

from . import IP, MAC, MODEL, PORT
from . import IP, HOST, MAC, MODEL, PORT


@pytest.mark.asyncio
Expand All @@ -36,6 +36,16 @@ async def test_connect(dev: AsyncMock):
assert dev.disconnect.call_count == 1


@pytest.mark.asyncio
async def test_connect_host(dev: AsyncMock):
"""Test connect succeeds."""
p = JvcProjector(HOST, port=PORT)
await p.connect()
assert p.ip == IP
await p.disconnect()
assert dev.disconnect.call_count == 1


@pytest.mark.asyncio
@pytest.mark.parametrize("dev", [{command.MODEL: None}], indirect=True)
async def test_unknown_model(dev: AsyncMock):
Expand Down

0 comments on commit 9522254

Please sign in to comment.