diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index 29eb07b3a90..2da2c5a6ea5 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -5,6 +5,7 @@ import logging import aioshelly import async_timeout +import voluptuous as vol from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -17,6 +18,7 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import aiohttp_client, device_registry, update_coordinator +import homeassistant.helpers.config_validation as cv from .const import ( AIOSHELLY_DEVICE_TIMEOUT_SEC, @@ -25,7 +27,9 @@ from .const import ( ATTR_DEVICE, BATTERY_DEVICES_WITH_PERMANENT_CONNECTION, COAP, + CONF_COAP_PORT, DATA_CONFIG_ENTRY, + DEFAULT_COAP_PORT, DEVICE, DOMAIN, EVENT_SHELLY_CLICK, @@ -43,10 +47,22 @@ PLATFORMS = ["binary_sensor", "cover", "light", "sensor", "switch"] SLEEPING_PLATFORMS = ["binary_sensor", "sensor"] _LOGGER = logging.getLogger(__name__) +COAP_SCHEMA = vol.Schema( + { + vol.Optional(CONF_COAP_PORT, default=DEFAULT_COAP_PORT): cv.port, + } +) +CONFIG_SCHEMA = vol.Schema({DOMAIN: COAP_SCHEMA}, extra=vol.ALLOW_EXTRA) + async def async_setup(hass: HomeAssistant, config: dict): """Set up the Shelly component.""" hass.data[DOMAIN] = {DATA_CONFIG_ENTRY: {}} + + conf = config.get(DOMAIN) + if conf is not None: + hass.data[DOMAIN][CONF_COAP_PORT] = conf[CONF_COAP_PORT] + return True diff --git a/homeassistant/components/shelly/const.py b/homeassistant/components/shelly/const.py index 2609b7cd57f..bff82057120 100644 --- a/homeassistant/components/shelly/const.py +++ b/homeassistant/components/shelly/const.py @@ -6,6 +6,9 @@ DEVICE = "device" DOMAIN = "shelly" REST = "rest" +CONF_COAP_PORT = "coap_port" +DEFAULT_COAP_PORT = 5683 + # Used in "_async_update_data" as timeout for polling data from devices. POLLING_TIMEOUT_SEC = 18 diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index 39134957fb9..7fabdb9bf8b 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -14,7 +14,9 @@ from homeassistant.util.dt import parse_datetime, utcnow from .const import ( BASIC_INPUTS_EVENTS_TYPES, COAP, + CONF_COAP_PORT, DATA_CONFIG_ENTRY, + DEFAULT_COAP_PORT, DOMAIN, SHBTN_INPUTS_EVENTS_TYPES, SHBTN_MODELS, @@ -190,7 +192,13 @@ def get_device_wrapper(hass: HomeAssistant, device_id: str): async def get_coap_context(hass): """Get CoAP context to be used in all Shelly devices.""" context = aioshelly.COAP() - await context.initialize() + port = DEFAULT_COAP_PORT + if DOMAIN in hass.data: + port = hass.data[DOMAIN].get(CONF_COAP_PORT, DEFAULT_COAP_PORT) + else: + port = DEFAULT_COAP_PORT + _LOGGER.info("Starting CoAP context with UDP port %s", port) + await context.initialize(port) @callback def shutdown_listener(ev):