2020-12-13 11:02:45 +00:00
|
|
|
"""Classes shared among Wemo entities."""
|
2021-03-18 14:08:35 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-04-20 15:40:41 +00:00
|
|
|
from collections.abc import Generator
|
2021-02-17 22:36:39 +00:00
|
|
|
import contextlib
|
2020-12-13 11:02:45 +00:00
|
|
|
import logging
|
|
|
|
|
2021-02-20 02:49:39 +00:00
|
|
|
from pywemo.exceptions import ActionException
|
2020-12-13 11:02:45 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2020-12-13 11:02:45 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
from .wemo_device import DeviceCoordinator
|
2020-12-13 11:02:45 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
class WemoEntity(CoordinatorEntity):
|
|
|
|
"""Common methods for Wemo entities."""
|
2021-02-17 22:36:39 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
def __init__(self, coordinator: DeviceCoordinator) -> None:
|
2020-12-13 11:02:45 +00:00
|
|
|
"""Initialize the WeMo device."""
|
2021-08-21 18:14:55 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
self.wemo = coordinator.wemo
|
|
|
|
self._device_info = coordinator.device_info
|
2020-12-13 11:02:45 +00:00
|
|
|
self._available = True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the device if any."""
|
|
|
|
return self.wemo.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
2021-08-21 18:14:55 +00:00
|
|
|
"""Return true if the device is available."""
|
|
|
|
return super().available and self._available
|
2021-06-16 10:00:34 +00:00
|
|
|
|
2020-12-13 11:02:45 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the id of this WeMo device."""
|
|
|
|
return self.wemo.serialnumber
|
|
|
|
|
|
|
|
@property
|
2021-05-01 22:37:19 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2020-12-13 11:02:45 +00:00
|
|
|
"""Return the device info."""
|
2021-06-16 10:00:34 +00:00
|
|
|
return self._device_info
|
2020-12-13 11:02:45 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
|
|
|
self._available = True
|
|
|
|
super()._handle_coordinator_update()
|
2020-12-13 11:02:45 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def _wemo_exception_handler(self, message: str) -> Generator[None, None, None]:
|
|
|
|
"""Wrap device calls to set `_available` when wemo exceptions happen."""
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
except ActionException as err:
|
|
|
|
_LOGGER.warning("Could not %s for %s (%s)", message, self.name, err)
|
|
|
|
self._available = False
|