mirror of
https://github.com/esphome/esphome.git
synced 2024-11-26 00:48:19 +01:00
Fix legacy zeroconf record update method (#5294)
This commit is contained in:
parent
c4adb30ab2
commit
45879e3100
1 changed files with 22 additions and 11 deletions
|
@ -1,19 +1,19 @@
|
||||||
|
import logging
|
||||||
import socket
|
import socket
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from typing import Optional
|
|
||||||
import logging
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from zeroconf import (
|
from zeroconf import (
|
||||||
DNSAddress,
|
DNSAddress,
|
||||||
DNSOutgoing,
|
DNSOutgoing,
|
||||||
DNSRecord,
|
|
||||||
DNSQuestion,
|
DNSQuestion,
|
||||||
|
RecordUpdate,
|
||||||
RecordUpdateListener,
|
RecordUpdateListener,
|
||||||
Zeroconf,
|
|
||||||
ServiceBrowser,
|
ServiceBrowser,
|
||||||
ServiceStateChange,
|
ServiceStateChange,
|
||||||
|
Zeroconf,
|
||||||
current_time_millis,
|
current_time_millis,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -24,17 +24,28 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class HostResolver(RecordUpdateListener):
|
class HostResolver(RecordUpdateListener):
|
||||||
|
"""Resolve a host name to an IP address."""
|
||||||
|
|
||||||
def __init__(self, name: str):
|
def __init__(self, name: str):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.address: Optional[bytes] = None
|
self.address: Optional[bytes] = None
|
||||||
|
|
||||||
def update_record(self, zc: Zeroconf, now: float, record: DNSRecord) -> None:
|
def async_update_records(
|
||||||
if record is None:
|
self, zc: Zeroconf, now: float, records: list[RecordUpdate]
|
||||||
return
|
) -> None:
|
||||||
if record.type == _TYPE_A:
|
"""Update multiple records in one shot.
|
||||||
assert isinstance(record, DNSAddress)
|
|
||||||
if record.name == self.name:
|
This will run in zeroconf's event loop thread so it
|
||||||
self.address = record.address
|
must be thread-safe.
|
||||||
|
"""
|
||||||
|
for record_update in records:
|
||||||
|
record, _ = record_update
|
||||||
|
if record is None:
|
||||||
|
continue
|
||||||
|
if record.type == _TYPE_A:
|
||||||
|
assert isinstance(record, DNSAddress)
|
||||||
|
if record.name == self.name:
|
||||||
|
self.address = record.address
|
||||||
|
|
||||||
def request(self, zc: Zeroconf, timeout: float) -> bool:
|
def request(self, zc: Zeroconf, timeout: float) -> bool:
|
||||||
now = time.time()
|
now = time.time()
|
||||||
|
|
Loading…
Reference in a new issue