2019-02-13 20:21:14 +00:00
|
|
|
"""Support for WeMo binary sensors."""
|
2018-12-19 07:12:32 +00:00
|
|
|
import asyncio
|
|
|
|
|
2022-05-23 06:43:42 +00:00
|
|
|
from pywemo import Insight, Maker, StandbyState
|
2021-08-22 18:09:22 +00:00
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
2021-12-20 00:09:30 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2020-01-19 20:56:31 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-12-20 00:09:30 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2016-03-04 16:35:46 +00:00
|
|
|
|
2020-01-19 20:56:31 +00:00
|
|
|
from .const import DOMAIN as WEMO_DOMAIN
|
2021-12-21 04:34:34 +00:00
|
|
|
from .entity import WemoBinaryStateEntity, WemoEntity
|
2021-12-20 00:09:30 +00:00
|
|
|
from .wemo_device import DeviceCoordinator
|
2019-04-12 06:37:45 +00:00
|
|
|
|
2016-03-04 16:35:46 +00:00
|
|
|
|
2021-12-20 00:09:30 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-01-19 20:56:31 +00:00
|
|
|
"""Set up WeMo binary sensors."""
|
2016-03-04 16:35:46 +00:00
|
|
|
|
2021-12-20 00:09:30 +00:00
|
|
|
async def _discovered_wemo(coordinator: DeviceCoordinator) -> None:
|
2020-01-19 20:56:31 +00:00
|
|
|
"""Handle a discovered Wemo device."""
|
2021-08-22 18:09:22 +00:00
|
|
|
if isinstance(coordinator.wemo, Insight):
|
|
|
|
async_add_entities([InsightBinarySensor(coordinator)])
|
|
|
|
elif isinstance(coordinator.wemo, Maker):
|
|
|
|
async_add_entities([MakerBinarySensor(coordinator)])
|
|
|
|
else:
|
|
|
|
async_add_entities([WemoBinarySensor(coordinator)])
|
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(
|
2021-07-19 08:46:09 +00:00
|
|
|
*(
|
2021-08-21 18:14:55 +00:00
|
|
|
_discovered_wemo(coordinator)
|
|
|
|
for coordinator in hass.data[WEMO_DOMAIN]["pending"].pop("binary_sensor")
|
2021-07-19 08:46:09 +00:00
|
|
|
)
|
2020-01-19 20:56:31 +00:00
|
|
|
)
|
2016-03-04 16:35:46 +00:00
|
|
|
|
|
|
|
|
2021-12-21 04:34:34 +00:00
|
|
|
class WemoBinarySensor(WemoBinaryStateEntity, BinarySensorEntity):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Representation a WeMo binary sensor."""
|
2016-03-04 16:35:46 +00:00
|
|
|
|
2021-08-22 18:09:22 +00:00
|
|
|
|
|
|
|
class MakerBinarySensor(WemoEntity, BinarySensorEntity):
|
|
|
|
"""Maker device's sensor port."""
|
|
|
|
|
|
|
|
_name_suffix = "Sensor"
|
2022-05-24 07:53:01 +00:00
|
|
|
wemo: Maker
|
2021-08-22 18:09:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if the Maker's sensor is pulled low."""
|
2022-05-24 07:53:01 +00:00
|
|
|
return self.wemo.has_sensor != 0 and self.wemo.sensor_state == 0
|
2021-08-22 18:09:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InsightBinarySensor(WemoBinarySensor):
|
|
|
|
"""Sensor representing the device connected to the Insight Switch."""
|
|
|
|
|
|
|
|
_name_suffix = "Device"
|
2022-05-24 07:53:01 +00:00
|
|
|
wemo: Insight
|
2021-08-22 18:09:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true device connected to the Insight Switch is on."""
|
2022-05-24 07:53:01 +00:00
|
|
|
return super().is_on and self.wemo.standby_state == StandbyState.ON
|