diff --git a/custom_components/xiaomi_miot/config_flow.py b/custom_components/xiaomi_miot/config_flow.py index e1ed07048..b77021ac3 100644 --- a/custom_components/xiaomi_miot/config_flow.py +++ b/custom_components/xiaomi_miot/config_flow.py @@ -25,6 +25,7 @@ MiotCloud, MiCloudException, ) +from .core.utils import async_analytics_track_event _LOGGER = logging.getLogger(__name__) DEFAULT_INTERVAL = 30 @@ -50,6 +51,7 @@ async def check_miio_device(hass, user_input, errors): device = MiioDevice(host, token) info = await hass.async_add_executor_job(device.info) except DeviceException: + device = None info = None errors['base'] = 'cannot_connect' _LOGGER.debug('Xiaomi Miot config flow: %s', { @@ -57,13 +59,32 @@ async def check_miio_device(hass, user_input, errors): 'miio_info': info, 'errors': errors, }) + model = '' if info is not None: if not user_input.get(CONF_MODEL): - user_input[CONF_MODEL] = str(info.model or '') + model = str(info.model or '') + user_input[CONF_MODEL] = model user_input['miio_info'] = dict(info.raw or {}) - miot_type = await MiotSpec.async_get_model_type(hass, user_input.get(CONF_MODEL)) + miot_type = await MiotSpec.async_get_model_type(hass, model) user_input['miot_type'] = miot_type user_input['unique_did'] = format_mac(info.mac_address) + if miot_type and device: + try: + pms = [ + {'did': 'miot', 'siid': 2, 'piid': 1}, + {'did': 'miot', 'siid': 2, 'piid': 2}, + {'did': 'miot', 'siid': 3, 'piid': 1}, + ] + results = device.get_properties(pms, property_getter='get_properties') or [] + for prop in results: + if not isinstance(prop, dict): + continue + if prop.get('code') == 0: + # Collect supported models in LAN + await async_analytics_track_event('miot', 'local', model) + break + except DeviceException: + pass return user_input diff --git a/custom_components/xiaomi_miot/core/utils.py b/custom_components/xiaomi_miot/core/utils.py index c34d96073..207e81e39 100644 --- a/custom_components/xiaomi_miot/core/utils.py +++ b/custom_components/xiaomi_miot/core/utils.py @@ -1,3 +1,7 @@ +import time +import requests +from functools import partial + class RC4: _idx = 0 @@ -39,3 +43,21 @@ def crypt(self, data): def init1024(self): self.crypt(bytes(1024)) return self + + +def analytics_track_event(event, action, label, value=0, node_id=''): + pms = { + 'id': '1280294351', + 'ei': '|'.join([event, action, label, f'{value}', node_id]), + 'p': 'https://miot-spec.com', + 't': 'Home Assistant', + 'rnd': int(time.time() / 2.67), + } + url = 'https://ei.cnzz.com/stat.htm' + return requests.get(url, params=pms) + + +async def async_analytics_track_event(self, *args, **kwargs): + return await self.hass.async_add_executor_job( + partial(self.analytics_track_event, *args, **kwargs) + )