Esempio n. 1
0
def build_meross_device_from_known_types(http_device_info: HttpDeviceInfo,
                                         manager) -> BaseDevice:
    """
    Builds a managed meross device object by guess its relative class based on the device type string.
    Note that this method is capable of building managed device wrappers only if the device type is
    reported within the _KNOWN_DEV_TYPES_CLASSES. If your device type is not known yet, you should rely on
    `build_meross_device_from_abilities()` instead.

    :param http_device_info:
    :param manager:
    :return:
    """
    _LOGGER.debug(
        f"Building managed device for {http_device_info.dev_name} ({http_device_info.uuid}) "
        f"from static known types ")
    dev_type = http_device_info.device_type.lower()
    target_clazz = _KNOWN_DEV_TYPES_CLASSES.get(dev_type)

    if target_clazz is None:
        _LOGGER.error(
            "Could not find any known device class for device type (%s).",
            http_device_info.device_type)
        raise UnknownDeviceType()

    return target_clazz(device_uuid=http_device_info.uuid,
                        manager=manager,
                        **http_device_info.to_dict())
Esempio n. 2
0
    async def async_list_devices(self) -> List[HttpDeviceInfo]:
        """
        Asks to the HTTP api to list the Meross device belonging to the given user account.

        :return:
        """
        result = await self._async_authenticated_post(_DEV_LIST, {}, cloud_creds=self._cloud_creds)
        return [HttpDeviceInfo.from_dict(x) for x in result]
Esempio n. 3
0
def build_meross_device_from_abilities(http_device_info: HttpDeviceInfo,
                                       device_abilities: dict,
                                       manager) -> BaseDevice:
    """
    Builds a managed meross device object given the specs reported by HTTP api and the abilities reported by the device
    itself.

    :param http_device_info:
    :param device_abilities:
    :param manager:
    :return:
    """
    # The current implementation of this library is based on the usage of pluggable Mixin classes on top of
    # a couple of base implementations.
    _LOGGER.debug(
        f"Building managed device for {http_device_info.dev_name} ({http_device_info.uuid}). "
        f"Reported abilities: {device_abilities}")

    # Check if we already have cached type for that device kind.
    cached_type = _lookup_cached_type(http_device_info.device_type,
                                      http_device_info.hdware_version,
                                      http_device_info.fmware_version)
    if cached_type is None:
        _LOGGER.debug(
            f"Could not find any cached type for {http_device_info.device_type},"
            f"{http_device_info.hdware_version},"
            f"{http_device_info.fmware_version}. It will be generated.")
        device_type_name = _caclulate_device_type_name(
            http_device_info.device_type, http_device_info.hdware_version,
            http_device_info.fmware_version)

        # Let's now pick the base class where to attach all the mixin.
        # We basically offer two possible base implementations:
        # - BaseMerossDevice: suitable for all non-hub devices
        # - HubMerossDevice: to be used when dealing with Hubs.
        # Unfortunately, it's not clear how we should discriminate an hub from a non-hub.
        # The current implementation decides which base class to use by looking at the presence
        # of 'Appliance.Digest.Hub' namespace within the exposed abilities.
        discriminating_ability = Namespace.SYSTEM_DIGEST_HUB.value
        base_class = BaseDevice
        if discriminating_ability in device_abilities:
            _LOGGER.warning(
                f"Device {http_device_info.dev_name} ({http_device_info.device_type}, "
                f"uuid {http_device_info.uuid}) reported ability {discriminating_ability}. "
                f"Assuming this is a full-featured HUB.")
            base_class = HubDevice

        cached_type = _build_cached_type(type_string=device_type_name,
                                         device_abilities=device_abilities,
                                         base_class=base_class)
        _dynamic_types[device_type_name] = cached_type

    component = cached_type(device_uuid=http_device_info.uuid,
                            manager=manager,
                            **http_device_info.to_dict())
    return component
Esempio n. 4
0
    async def async_list_devices(self, *args,
                                 **kwargs) -> List[HttpDeviceInfo]:
        """
        Asks to the HTTP api to list the Meross device belonging to the given user account.

        :return: a list of `HttpDeviceInfo`
        """
        url = _DEV_LIST % self.api_url
        result = await MerossHttpClient._async_authenticated_post(
            url=url,
            params_data={},
            cloud_creds=self._cloud_creds,
            http_proxy=self._http_proxy,
            ua_header=self._ua_header,
            app_type=self._app_type,
            app_version=self._app_version,
            stats_counter=self._stats_counter)
        return [HttpDeviceInfo.from_dict(x) for x in result]