1 """Code to handle a Xiaomi Device."""
5 from functools
import partial
9 from miio
import DeviceException
17 DataUpdateCoordinator,
20 from .const
import ATTR_AVAILABLE, DOMAIN
22 _LOGGER = logging.getLogger(__name__)
26 """Representation of a base Xiaomi Miio Entity."""
28 def __init__(self, name, device, entry, unique_id):
29 """Initialize the Xiaomi Miio Device."""
31 self.
_model_model = entry.data[CONF_MODEL]
32 self.
_mac_mac = entry.data[CONF_MAC]
40 """Return an unique ID."""
45 """Return the name of this entity, if any."""
46 return self.
_name_name
50 """Return the device info."""
52 identifiers={(DOMAIN, self.
_device_id_device_id)},
53 manufacturer=
"Xiaomi",
58 if self.
_mac_mac
is not None:
59 device_info[ATTR_CONNECTIONS] = {(dr.CONNECTION_NETWORK_MAC, self.
_mac_mac)}
67 """Representation of a base a coordinated Xiaomi Miio Entity."""
69 _attr_has_entity_name =
True
71 def __init__(self, device, entry, unique_id, coordinator):
72 """Initialize the coordinated Xiaomi Miio Device."""
75 self._model = entry.data[CONF_MODEL]
76 self._mac = entry.data[CONF_MAC]
77 self._device_id = entry.unique_id
78 self._device_name = entry.title
79 self._unique_id = unique_id
83 """Return an unique ID."""
84 return self._unique_id
88 """Return the device info."""
90 identifiers={(DOMAIN, self._device_id)},
91 manufacturer=
"Xiaomi",
93 name=self._device_name,
96 if self._mac
is not None:
97 device_info[ATTR_CONNECTIONS] = {(dr.CONNECTION_NETWORK_MAC, self._mac)}
102 """Call a miio device command handling error messages."""
104 result = await self.hass.async_add_executor_job(
105 partial(func, *args, **kwargs)
107 except DeviceException
as exc:
109 _LOGGER.error(mask_error, exc)
113 _LOGGER.debug(
"Response received from miio device: %s", result)
118 value = getattr(state, attribute)
119 if isinstance(value, Enum):
121 if isinstance(value, datetime.timedelta):
122 return cls._parse_time_delta(value)
123 if isinstance(value, datetime.time):
124 return cls._parse_datetime_time(value)
125 if isinstance(value, datetime.datetime):
126 return cls._parse_datetime_datetime(value)
129 _LOGGER.debug(
"Attribute %s is None, this is unexpected", attribute)
135 return int(timedelta.total_seconds())
139 time = datetime.datetime.now().replace(
140 hour=initial_time.hour, minute=initial_time.minute, second=0, microsecond=0
143 if time < datetime.datetime.now():
144 time += datetime.timedelta(days=1)
146 return time.isoformat()
150 return time.isoformat()
154 """Representation of a base Xiaomi Gateway Device."""
156 def __init__(self, coordinator, sub_device, entry):
157 """Initialize the Xiaomi Gateway Device."""
162 self.
_name_name = f
"{sub_device.name} ({sub_device.sid})"
166 """Return an unique ID."""
171 """Return the name of this entity, if any."""
172 return self.
_name_name
176 """Return the device info of the gateway."""
178 identifiers={(DOMAIN, self.
_sub_device_sub_device.sid)},
179 via_device=(DOMAIN, self.
_entry_entry.unique_id),
180 manufacturer=
"Xiaomi",
183 sw_version=self.
_sub_device_sub_device.firmware_version,
184 hw_version=self.
_sub_device_sub_device.zigbee_model,
189 """Return if entity is available."""
190 if self.coordinator.data
is None:
193 return self.coordinator.data[ATTR_AVAILABLE]
DeviceInfo device_info(self)
def __init__(self, coordinator, sub_device, entry)
def __init__(self, name, device, entry, unique_id)
DeviceInfo device_info(self)
str _parse_datetime_time(datetime.time initial_time)
int _parse_time_delta(datetime.timedelta timedelta)
DeviceInfo device_info(self)
def _extract_value_from_attribute(cls, state, attribute)
def _try_command(self, mask_error, func, *args, **kwargs)
def __init__(self, device, entry, unique_id, coordinator)
str _parse_datetime_datetime(datetime.datetime time)