2019-02-13 20:21:14 +00:00
|
|
|
"""Support for WeMo binary sensors."""
|
2018-12-19 07:12:32 +00:00
|
|
|
import asyncio
|
2016-03-04 16:35:46 +00:00
|
|
|
import logging
|
2018-12-19 07:12:32 +00:00
|
|
|
|
2020-05-17 20:13:38 +00:00
|
|
|
from pywemo.ouimeaux_device.api.service import ActionException
|
2016-03-04 16:35:46 +00:00
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
2020-01-19 20:56:31 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2016-03-04 16:35:46 +00:00
|
|
|
|
2020-01-19 20:56:31 +00:00
|
|
|
from .const import DOMAIN as WEMO_DOMAIN
|
2020-12-13 11:02:45 +00:00
|
|
|
from .entity import WemoSubscriptionEntity
|
2019-04-12 06:37:45 +00:00
|
|
|
|
2016-03-04 16:35:46 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-01-19 20:56:31 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up WeMo binary sensors."""
|
2016-03-04 16:35:46 +00:00
|
|
|
|
2020-01-19 20:56:31 +00:00
|
|
|
async def _discovered_wemo(device):
|
|
|
|
"""Handle a discovered Wemo device."""
|
|
|
|
async_add_entities([WemoBinarySensor(device)])
|
2018-08-16 14:14:54 +00:00
|
|
|
|
2020-01-19 20:56:31 +00:00
|
|
|
async_dispatcher_connect(hass, f"{WEMO_DOMAIN}.binary_sensor", _discovered_wemo)
|
2016-03-04 16:35:46 +00:00
|
|
|
|
2020-01-19 20:56:31 +00:00
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
|
|
|
_discovered_wemo(device)
|
|
|
|
for device in hass.data[WEMO_DOMAIN]["pending"].pop("binary_sensor")
|
|
|
|
]
|
|
|
|
)
|
2016-03-04 16:35:46 +00:00
|
|
|
|
|
|
|
|
2020-12-13 11:02:45 +00:00
|
|
|
class WemoBinarySensor(WemoSubscriptionEntity, BinarySensorEntity):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Representation a WeMo binary sensor."""
|
2016-03-04 16:35:46 +00:00
|
|
|
|
2018-12-19 07:12:32 +00:00
|
|
|
def _update(self, force_update=True):
|
|
|
|
"""Update the sensor state."""
|
|
|
|
try:
|
|
|
|
self._state = self.wemo.get_state(force_update)
|
|
|
|
|
|
|
|
if not self._available:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.info("Reconnected to %s", self.name)
|
2018-12-19 07:12:32 +00:00
|
|
|
self._available = True
|
2021-02-17 08:07:22 +00:00
|
|
|
except ActionException as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning("Could not update status for %s (%s)", self.name, err)
|
2018-12-19 07:12:32 +00:00
|
|
|
self._available = False
|