Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Classes shared among Wemo entities."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Generator
6 import contextlib
7 import logging
8 
9 from pywemo.exceptions import ActionException
10 
11 from homeassistant.helpers.device_registry import DeviceInfo
12 from homeassistant.helpers.update_coordinator import CoordinatorEntity
13 
14 from .coordinator import DeviceCoordinator
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 
19 class WemoEntity(CoordinatorEntity[DeviceCoordinator]):
20  """Common methods for Wemo entities."""
21 
22  # Most pyWeMo devices are associated with a single Home Assistant entity. When
23  # that is not the case, name_suffix & unique_id_suffix can be used to provide
24  # names and unique ids for additional Home Assistant entities.
25  _name_suffix: str | None = None
26  _unique_id_suffix: str | None = None
27 
28  def __init__(self, coordinator: DeviceCoordinator) -> None:
29  """Initialize the WeMo device."""
30  super().__init__(coordinator)
31  self.wemowemowemo = coordinator.wemo
32  self._device_info_device_info = coordinator.device_info
33 
34  @property
35  def name_suffix(self) -> str | None:
36  """Suffix to append to the WeMo device name."""
37  return self._name_suffix
38 
39  @property
40  def name(self) -> str:
41  """Return the name of the device if any."""
42  wemo_name: str = self.wemowemowemo.name
43  if suffix := self.name_suffixname_suffix:
44  return f"{wemo_name} {suffix}"
45  return wemo_name
46 
47  @property
48  def unique_id_suffix(self) -> str | None:
49  """Suffix to append to the WeMo device's unique ID."""
50  if self._unique_id_suffix is None and self.name_suffixname_suffix is not None:
51  return self.name_suffixname_suffix.lower()
52  return self._unique_id_suffix
53 
54  @property
55  def unique_id(self) -> str:
56  """Return the id of this WeMo device."""
57  serial_number: str = self.wemowemowemo.serial_number
58  if suffix := self.unique_id_suffixunique_id_suffix:
59  return f"{serial_number}_{suffix}"
60  return serial_number
61 
62  @property
63  def device_info(self) -> DeviceInfo:
64  """Return the device info."""
65  return self._device_info_device_info
66 
67  @contextlib.contextmanager
68  def _wemo_call_wrapper(self, message: str) -> Generator[None]:
69  """Wrap calls to the device that change its state.
70 
71  1. Takes care of making available=False when communications with the
72  device fails.
73  2. Ensures all entities sharing the same coordinator are aware of
74  updates to the device state.
75  """
76  try:
77  yield
78  except ActionException as err:
79  _LOGGER.warning("Could not %s for %s (%s)", message, self.namenamename, err)
80  self.coordinator.last_exception = err
81  self.coordinator.last_update_success = False # Used for self.available.
82  finally:
83  self.hasshasshass.add_job(self.coordinator.async_update_listeners)
84 
85 
87  """Base for devices that return on/off state via device.get_state()."""
88 
89  @property
90  def is_on(self) -> bool:
91  """Return true if the state is on."""
92  return self.wemowemowemo.get_state() != 0
Generator[None] _wemo_call_wrapper(self, str message)
Definition: entity.py:68
None __init__(self, DeviceCoordinator coordinator)
Definition: entity.py:28
str|float get_state(dict[str, float] data, str key)
Definition: sensor.py:26