core/homeassistant/components/xiaomi_miio/device.py

56 lines
1.6 KiB
Python

"""Code to handle a Xiaomi Device."""
import logging
from construct.core import ChecksumError
from miio import Device, DeviceException
from .const import AuthException, SetupException
_LOGGER = logging.getLogger(__name__)
class ConnectXiaomiDevice:
"""Class to async connect to a Xiaomi Device."""
def __init__(self, hass):
"""Initialize the entity."""
self._hass = hass
self._device = None
self._device_info = None
@property
def device(self):
"""Return the class containing all connections to the device."""
return self._device
@property
def device_info(self):
"""Return the class containing device info."""
return self._device_info
async def async_connect_device(self, host, token):
"""Connect to the Xiaomi Device."""
_LOGGER.debug("Initializing with host %s (token %s...)", host, token[:5])
try:
self._device = Device(host, token)
# get the device info
self._device_info = await self._hass.async_add_executor_job(
self._device.info
)
except DeviceException as error:
if isinstance(error.__cause__, ChecksumError):
raise AuthException(error) from error
raise SetupException(
f"DeviceException during setup of xiaomi device with host {host}"
) from error
_LOGGER.debug(
"%s %s %s detected",
self._device_info.model,
self._device_info.firmware_version,
self._device_info.hardware_version,
)