Add yaml key to Shelly to allow CoAP port customization (#48729)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
pull/50268/head
Simone Chemelli 2021-05-07 22:23:29 +02:00 committed by GitHub
parent 552ca1c57b
commit cf96d86985
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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):