core/homeassistant/components/wemo/binary_sensor.py

38 lines
1.1 KiB
Python
Raw Normal View History

"""Support for WeMo binary sensors."""
import asyncio
2016-03-04 16:35:46 +00:00
import logging
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.helpers.dispatcher import async_dispatcher_connect
2016-03-04 16:35:46 +00:00
from .const import DOMAIN as WEMO_DOMAIN
from .entity import WemoSubscriptionEntity
2016-03-04 16:35:46 +00:00
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up WeMo binary sensors."""
2016-03-04 16:35:46 +00:00
async def _discovered_wemo(device):
"""Handle a discovered Wemo device."""
async_add_entities([WemoBinarySensor(device)])
async_dispatcher_connect(hass, f"{WEMO_DOMAIN}.binary_sensor", _discovered_wemo)
2016-03-04 16:35:46 +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
class WemoBinarySensor(WemoSubscriptionEntity, BinarySensorEntity):
"""Representation a WeMo binary sensor."""
2016-03-04 16:35:46 +00:00
def _update(self, force_update=True):
"""Update the sensor state."""
with self._wemo_exception_handler("update status"):
self._state = self.wemo.get_state(force_update)