Skip to content

Commit

Permalink
Allow setting of CoAP listening IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
chemelli74 committed Feb 20, 2024
1 parent f108ecd commit ec642b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 10 additions & 6 deletions aioshelly/block_device/coap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import logging
import socket
import struct
from collections.abc import Callable
from enum import Enum, auto
from types import TracebackType
from typing import Callable, cast
from typing import cast

from ..const import DEFAULT_COAP_PORT, DEFAULT_IP_ADDRESS
from ..json import JSONDecodeError, json_loads

COAP_OPTION_DEVICE_ID = 3332
Expand Down Expand Up @@ -111,12 +113,12 @@ def _read_extended_field_value(value: int, raw_data: bytes) -> tuple[int, bytes]
raise InvalidMessage("Option contained partial payload marker.")


def socket_init(socket_port: int) -> socket.socket:
def socket_init(socket_ip: str, socket_port: int) -> socket.socket:
"""Init UDP socket to send/receive data with Shelly devices."""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("", socket_port))
_LOGGER.debug("Socket initialized on port %s", socket_port)
sock.bind((socket_ip, socket_port))
_LOGGER.debug("Socket initialized on %s:%s", socket_ip, socket_port)
mreq = struct.pack("=4sl", socket.inet_aton("224.0.1.187"), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
sock.setblocking(False)
Expand All @@ -134,10 +136,12 @@ def __init__(self, message_received: Callable | None = None) -> None:
self.subscriptions: dict[str, Callable] = {}
self.transport: asyncio.DatagramTransport | None = None

async def initialize(self, socket_port: int = 5683) -> None:
async def initialize(
self, socket_ip: str = DEFAULT_IP_ADDRESS, socket_port: int = DEFAULT_COAP_PORT
) -> None:
"""Initialize the COAP manager."""
loop = asyncio.get_running_loop()
self.sock = socket_init(socket_port)
self.sock = socket_init(socket_ip, socket_port)
await loop.create_datagram_endpoint(lambda: self, sock=self.sock)

async def request(self, ip: str, path: str) -> None:
Expand Down
4 changes: 4 additions & 0 deletions aioshelly/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@

WS_HEARTBEAT = 55

# Default network settings for gen1 devices ( CoAP )
DEFAULT_COAP_PORT = 5683
DEFAULT_IP_ADDRESS = "0.0.0.0"

# Default Gen2 outbound websocket API URL
WS_API_URL = "/api/shelly/ws"

Expand Down

0 comments on commit ec642b2

Please sign in to comment.