Skip to content

Commit

Permalink
Fix legacy zeroconf record update method
Browse files Browse the repository at this point in the history
The first step of removing the legacy update_record is in
python-zeroconf/python-zeroconf#1231

This prepares esphome for this as its been deprecated for a few years and
this one was missed
  • Loading branch information
bdraco committed Aug 22, 2023
1 parent f814b6d commit e580c2d
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions esphome/zeroconf.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import logging
import socket
import threading
import time
from typing import Optional
import logging
from dataclasses import dataclass
from typing import Optional

from zeroconf import (
DNSAddress,
DNSOutgoing,
DNSRecord,
DNSQuestion,
RecordUpdate,
RecordUpdateListener,
Zeroconf,
ServiceBrowser,
ServiceStateChange,
Zeroconf,
current_time_millis,
)

Expand All @@ -24,17 +24,28 @@


class HostResolver(RecordUpdateListener):
"""Resolve a host name to an IP address."""

def __init__(self, name: str):
self.name = name
self.address: Optional[bytes] = None

def update_record(self, zc: Zeroconf, now: float, record: DNSRecord) -> None:
if record is None:
return
if record.type == _TYPE_A:
assert isinstance(record, DNSAddress)
if record.name == self.name:
self.address = record.address
def async_update_records(
self, zc: Zeroconf, now: float, records: list[RecordUpdate]
) -> None:
"""Update multiple records in one shot.
This will run in zeroconf's event loop thread so it
must be thread-safe.
"""
for record_update in records:
record, _ = record_update
if record is None:
return
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:
now = time.time()
Expand Down

0 comments on commit e580c2d

Please sign in to comment.