Improve Wemo config entry support, add device info (#30963)

* Improve config entry support, add device info

* async_dispatch_connect

* Fix I/O in event loop

* Do not raise PlatformNotReady inside dispatcher

* Make main discovery process async

* Do discovery as part of set up.

* Greatly simplify set up

* Add parallel updates to fan&switch

* mini cleanup

* Address comments
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-01-19 12:56:31 -08:00
1 parent 90e811df20
commit f14d34560e
5 files changed
+206 -189

No files matched your search

+22 -22
View File
@@ -4,18 +4,16 @@ from datetime import datetime, timedelta
import logging
import async_timeout
from pywemo import discovery
import requests
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import STATE_OFF, STATE_ON, STATE_STANDBY, STATE_UNKNOWN
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util import convert
from . import SUBSCRIPTION_REGISTRY
from .const import DOMAIN
from .const import DOMAIN as WEMO_DOMAIN
SCAN_INTERVAL = timedelta(seconds=10)
PARALLEL_UPDATES = 0
_LOGGER = logging.getLogger(__name__)
@@ -32,24 +30,21 @@ WEMO_OFF = 0
WEMO_STANDBY = 8
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up discovered WeMo switches."""
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up WeMo switches."""
if discovery_info is not None:
location = discovery_info["ssdp_description"]
mac = discovery_info["mac_address"]
async def _discovered_wemo(device):
"""Handle a discovered Wemo device."""
async_add_entities([WemoSwitch(device)])
try:
device = discovery.device_from_description(location, mac)
except (
requests.exceptions.ConnectionError,
requests.exceptions.Timeout,
) as err:
_LOGGER.error("Unable to access %s (%s)", location, err)
raise PlatformNotReady
async_dispatcher_connect(hass, f"{WEMO_DOMAIN}.switch", _discovered_wemo)
if device:
add_entities([WemoSwitch(device)])
await asyncio.gather(
*[
_discovered_wemo(device)
for device in hass.data[WEMO_DOMAIN]["pending"].pop("switch")
]
)
class WemoSwitch(SwitchDevice):
@@ -97,7 +92,12 @@ class WemoSwitch(SwitchDevice):
@property
def device_info(self):
"""Return the device info."""
return {"name": self._name, "identifiers": {(DOMAIN, self._serialnumber)}}
return {
"name": self.wemo.name,
"identifiers": {(WEMO_DOMAIN, self.wemo.serialnumber)},
"model": self.wemo.model_name,
"manufacturer": "Belkin",
}
@property
def device_state_attributes(self):
@@ -200,7 +200,7 @@ class WemoSwitch(SwitchDevice):
# Define inside async context so we know our event loop
self._update_lock = asyncio.Lock()
registry = SUBSCRIPTION_REGISTRY
registry = self.hass.data[WEMO_DOMAIN]["registry"]
await self.hass.async_add_job(registry.register, self.wemo)
registry.on(self.wemo, None, self._subscription_callback)