Skip to content

DHCP API

DHCP configuration classes.

DHCPConfig

DHCPConfig

Bases: UCISection

DHCP configuration manager.

Source code in src/wrtkit/dhcp.py
class DHCPConfig(UCISection):
    """DHCP configuration manager."""

    sections: List[DHCPSection] = Field(default_factory=list)
    hosts: List[DHCPHost] = Field(default_factory=list)
    dnsmasq: Optional[DnsmasqSection] = None
    remote_policy: Optional[RemotePolicy] = None

    def __init__(self, **data: Any) -> None:
        super().__init__(**data)
        self._package = "dhcp"
        self._section = ""
        self._section_type = ""

    def add_dhcp(self, dhcp: DHCPSection) -> "DHCPConfig":
        """Add a DHCP section and return self for chaining."""
        self.sections.append(dhcp)
        return self

    def add_host(self, host: DHCPHost) -> "DHCPConfig":
        """Add a DHCP static lease (host) and return self for chaining."""
        self.hosts.append(host)
        return self

    def set_dnsmasq(self, dnsmasq: DnsmasqSection) -> "DHCPConfig":
        """Set the dnsmasq configuration and return self for chaining."""
        self.dnsmasq = dnsmasq
        return self

    def get_commands(self) -> List[UCICommand]:
        """Get all UCI commands for DHCP configuration."""
        commands = []
        if self.dnsmasq is not None:
            commands.extend(self.dnsmasq.get_commands())
        for section in self.sections:
            commands.extend(section.get_commands())
        for host in self.hosts:
            commands.extend(host.get_commands())
        return commands

add_dhcp(dhcp)

Add a DHCP section and return self for chaining.

Source code in src/wrtkit/dhcp.py
def add_dhcp(self, dhcp: DHCPSection) -> "DHCPConfig":
    """Add a DHCP section and return self for chaining."""
    self.sections.append(dhcp)
    return self

get_commands()

Get all UCI commands for DHCP configuration.

Source code in src/wrtkit/dhcp.py
def get_commands(self) -> List[UCICommand]:
    """Get all UCI commands for DHCP configuration."""
    commands = []
    if self.dnsmasq is not None:
        commands.extend(self.dnsmasq.get_commands())
    for section in self.sections:
        commands.extend(section.get_commands())
    for host in self.hosts:
        commands.extend(host.get_commands())
    return commands

DHCPSection

DHCPSection

Bases: UCISection

Represents a DHCP configuration section.

Source code in src/wrtkit/dhcp.py
class DHCPSection(UCISection):
    """Represents a DHCP configuration section."""

    interface: Optional[str] = None
    start: Optional[int] = None
    limit: Optional[int] = None
    leasetime: Optional[str] = None
    ignore: Optional[bool] = None

    def __init__(self, dhcp_name: str, **data: Any) -> None:
        super().__init__(**data)
        self._package = "dhcp"
        self._section = dhcp_name
        self._section_type = "dhcp"

    # Immutable builder methods (composable)
    def with_interface(self, value: str) -> "DHCPSection":
        """Set the interface for this DHCP server (returns new copy)."""
        return self.model_copy(update={"interface": value})

    def with_start(self, value: int) -> "DHCPSection":
        """Set the start of the IP address range (returns new copy)."""
        return self.model_copy(update={"start": value})

    def with_limit(self, value: int) -> "DHCPSection":
        """Set the number of addresses in the pool (returns new copy)."""
        return self.model_copy(update={"limit": value})

    def with_leasetime(self, value: str) -> "DHCPSection":
        """Set the lease time (e.g., '12h', '24h') (returns new copy)."""
        return self.model_copy(update={"leasetime": value})

    def with_ignore(self, value: bool) -> "DHCPSection":
        """Enable or disable this DHCP server (returns new copy)."""
        return self.model_copy(update={"ignore": value})

    # Convenience builder methods for common configurations
    def with_range(self, start: int, limit: int, leasetime: str = "12h") -> "DHCPSection":
        """Configure DHCP address range (returns new copy)."""
        return self.model_copy(update={"start": start, "limit": limit, "leasetime": leasetime})

with_interface(value)

Set the interface for this DHCP server (returns new copy).

Source code in src/wrtkit/dhcp.py
def with_interface(self, value: str) -> "DHCPSection":
    """Set the interface for this DHCP server (returns new copy)."""
    return self.model_copy(update={"interface": value})

with_start(value)

Set the start of the IP address range (returns new copy).

Source code in src/wrtkit/dhcp.py
def with_start(self, value: int) -> "DHCPSection":
    """Set the start of the IP address range (returns new copy)."""
    return self.model_copy(update={"start": value})

with_limit(value)

Set the number of addresses in the pool (returns new copy).

Source code in src/wrtkit/dhcp.py
def with_limit(self, value: int) -> "DHCPSection":
    """Set the number of addresses in the pool (returns new copy)."""
    return self.model_copy(update={"limit": value})

with_leasetime(value)

Set the lease time (e.g., '12h', '24h') (returns new copy).

Source code in src/wrtkit/dhcp.py
def with_leasetime(self, value: str) -> "DHCPSection":
    """Set the lease time (e.g., '12h', '24h') (returns new copy)."""
    return self.model_copy(update={"leasetime": value})

with_ignore(value)

Enable or disable this DHCP server (returns new copy).

Source code in src/wrtkit/dhcp.py
def with_ignore(self, value: bool) -> "DHCPSection":
    """Enable or disable this DHCP server (returns new copy)."""
    return self.model_copy(update={"ignore": value})

with_range(start, limit, leasetime='12h')

Configure DHCP address range (returns new copy).

Source code in src/wrtkit/dhcp.py
def with_range(self, start: int, limit: int, leasetime: str = "12h") -> "DHCPSection":
    """Configure DHCP address range (returns new copy)."""
    return self.model_copy(update={"start": start, "limit": limit, "leasetime": leasetime})

Usage Example

from wrtkit import UCIConfig
from wrtkit.dhcp import DHCPSection

config = UCIConfig()

# Create DHCP section
section = DHCPSection("lan")\
    .with_interface("lan")\
    .with_start(100)\
    .with_limit(150)\
    .with_leasetime("12h")

config.dhcp.add_dhcp(section)

See Also