async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the sensor from config.""" from miio import AirQualityMonitor, DeviceException if DATA_KEY not in hass.data: hass.data[DATA_KEY] = {} host = config.get(CONF_HOST) name = config.get(CONF_NAME) token = config.get(CONF_TOKEN) _LOGGER.info("Initializing with host %s (token %s...)", host, token[:5]) try: air_quality_monitor = AirQualityMonitor(host, token) device_info = air_quality_monitor.info() model = device_info.model unique_id = "{}-{}".format(model, device_info.mac_address) _LOGGER.info("%s %s %s detected", model, device_info.firmware_version, device_info.hardware_version) device = XiaomiAirQualityMonitor(name, air_quality_monitor, model, unique_id) except DeviceException: raise PlatformNotReady hass.data[DATA_KEY][host] = device async_add_entities([device], update_before_add=True)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the sensor from config.""" from miio import AirQualityMonitor, DeviceException if DATA_KEY not in hass.data: hass.data[DATA_KEY] = {} host = config.get(CONF_HOST) name = config.get(CONF_NAME) token = config.get(CONF_TOKEN) _LOGGER.info("Initializing with host %s (token %s...)", host, token[:5]) try: air_quality_monitor = AirQualityMonitor(host, token) device_info = air_quality_monitor.info() model = device_info.model unique_id = "{}-{}".format(model, device_info.mac_address) _LOGGER.info("%s %s %s detected", model, device_info.firmware_version, device_info.hardware_version) device = XiaomiAirQualityMonitor( name, air_quality_monitor, model, unique_id) except DeviceException: raise PlatformNotReady hass.data[DATA_KEY][host] = device async_add_entities([device], update_before_add=True)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the sensor from config.""" if DATA_KEY not in hass.data: hass.data[DATA_KEY] = {} host = config[CONF_HOST] token = config[CONF_TOKEN] name = config[CONF_NAME] _LOGGER.info("Initializing with host %s (token %s...)", host, token[:5]) try: air_quality_monitor = AirQualityMonitor(host, token) device_info = await hass.async_add_executor_job( air_quality_monitor.info) model = device_info.model unique_id = f"{model}-{device_info.mac_address}" _LOGGER.info( "%s %s %s detected", model, device_info.firmware_version, device_info.hardware_version, ) device = XiaomiAirQualityMonitor(name, air_quality_monitor, model, unique_id) except DeviceException as ex: raise PlatformNotReady from ex hass.data[DATA_KEY][host] = device async_add_entities([device], update_before_add=True)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the sensor from config.""" host = config[CONF_HOST] token = config[CONF_TOKEN] name = config[CONF_NAME] _LOGGER.info("Initializing with host %s (token %s...)", host, token[:5]) miio_device = Device(host, token) try: device_info = await hass.async_add_executor_job(miio_device.info) except DeviceException: raise PlatformNotReady model = device_info.model unique_id = f"{model}-{device_info.mac_address}" _LOGGER.debug( "%s %s %s detected", model, device_info.firmware_version, device_info.hardware_version, ) device = AirMonitorB1(name, AirQualityMonitor(host, token, model=model), unique_id) async_add_entities([device], update_before_add=True)
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up the Xiaomi Air Quality from a config entry.""" entities = [] if config_entry.data[CONF_FLOW_TYPE] == CONF_DEVICE: host = config_entry.data[CONF_HOST] token = config_entry.data[CONF_TOKEN] name = config_entry.title model = config_entry.data[CONF_MODEL] unique_id = config_entry.unique_id _LOGGER.debug("Initializing with host %s (token %s...)", host, token[:5]) device = AirQualityMonitor(host, token, model=model) if model == MODEL_AIRQUALITYMONITOR_S1: entities.append(AirMonitorS1(name, device, config_entry, unique_id)) elif model == MODEL_AIRQUALITYMONITOR_B1: entities.append(AirMonitorB1(name, device, config_entry, unique_id)) elif model == MODEL_AIRQUALITYMONITOR_V1: entities.append(AirMonitorV1(name, device, config_entry, unique_id)) else: _LOGGER.warning( "AirQualityMonitor model '%s' is not yet supported", model) async_add_entities(entities, update_before_add=True)
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up the Xiaomi sensor from a config entry.""" entities = [] if config_entry.data[CONF_FLOW_TYPE] == CONF_GATEWAY: gateway = hass.data[DOMAIN][config_entry.entry_id][CONF_GATEWAY] # Gateway illuminance sensor if gateway.model not in [ GATEWAY_MODEL_AC_V1, GATEWAY_MODEL_AC_V2, GATEWAY_MODEL_AC_V3, GATEWAY_MODEL_EU, ]: entities.append( XiaomiGatewayIlluminanceSensor( gateway, config_entry.title, config_entry.unique_id ) ) # Gateway sub devices sub_devices = gateway.devices coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR] for sub_device in sub_devices.values(): sensor_variables = set(sub_device.status) & set(GATEWAY_SENSOR_TYPES) if sensor_variables: entities.extend( [ XiaomiGatewaySensor( coordinator, sub_device, config_entry, variable ) for variable in sensor_variables ] ) if config_entry.data[CONF_FLOW_TYPE] == CONF_DEVICE: host = config_entry.data[CONF_HOST] token = config_entry.data[CONF_TOKEN] name = config_entry.title unique_id = config_entry.unique_id _LOGGER.debug("Initializing with host %s (token %s...)", host, token[:5]) device = AirQualityMonitor(host, token) entities.append(XiaomiAirQualityMonitor(name, device, config_entry, unique_id)) async_add_entities(entities, update_before_add=True)
def setup_platform(hass, config, add_entities, discovery_info=None): """Perform the setup for XiaoMi Multifunction Air Monitor.""" from miio import AirQualityMonitor, DeviceException host = config.get(CONF_HOST) name = config.get(CONF_NAME) token = config.get(CONF_TOKEN) device_class = config.get(CONF_DEVICE_CLASS) _LOGGER.info("Initializing Xiaomi Mi Multifunction Air Monitor with host %s (token %s...)", host, token[:5]) entities = [] try: airQualityMonitor = AirQualityMonitor(host, token, model=device_class) airQualityMonitorSensor = XiaomiAirQualityMonitorSensor(airQualityMonitor, name) entities.append(airQualityMonitorSensor) except DeviceException: raise PlatformNotReady add_entities(entities)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the sensor from config.""" host = config[CONF_HOST] token = config[CONF_TOKEN] name = config[CONF_NAME] _LOGGER.info("Initializing with host %s (token %s...)", host, token[:5]) miio_device = Device(host, token) try: device_info = await hass.async_add_executor_job(miio_device.info) except DeviceException as ex: raise PlatformNotReady from ex model = device_info.model unique_id = f"{model}-{device_info.mac_address}" _LOGGER.debug( "%s %s %s detected", model, device_info.firmware_version, device_info.hardware_version, ) device = AirQualityMonitor(host, token, model=model) if model == MODEL_AIRQUALITYMONITOR_S1: entity = AirMonitorS1(name, device, unique_id) elif model == MODEL_AIRQUALITYMONITOR_B1: entity = AirMonitorB1(name, device, unique_id) elif model == MODEL_AIRQUALITYMONITOR_V1: entity = AirMonitorV1(name, device, unique_id) else: raise NoEntitySpecifiedError(f"Not support for entity {unique_id}") async_add_entities([entity], update_before_add=True)
def setup_platform(hass, config, add_devices, discovery_info=None): """Perform the setup for Xiaomi air quality monitor.""" from miio import AirQualityMonitor, DeviceException host = config.get(CONF_HOST) name = config.get(CONF_NAME) token = config.get(CONF_TOKEN) _LOGGER.info( "Initializing Xiaomi air quality monitor with host %s (token %s...)", host, token[:5]) devices = [] try: airQualityMonitor = AirQualityMonitor(host, token) airQualityMonitorSensor = XiaomiAirQualityMonitorSensor( airQualityMonitor, name) devices.append(airQualityMonitorSensor) except DeviceException: raise PlatformNotReady add_devices(devices)
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up the Xiaomi sensor from a config entry.""" entities = [] if config_entry.data[CONF_FLOW_TYPE] == CONF_GATEWAY: gateway = hass.data[DOMAIN][config_entry.entry_id][CONF_GATEWAY] # Gateway illuminance sensor if gateway.model not in [ GATEWAY_MODEL_AC_V1, GATEWAY_MODEL_AC_V2, GATEWAY_MODEL_AC_V3, GATEWAY_MODEL_AQARA, GATEWAY_MODEL_EU, ]: description = SENSOR_TYPES[ATTR_ILLUMINANCE] entities.append( XiaomiGatewayIlluminanceSensor(gateway, config_entry.title, config_entry.unique_id, description)) # Gateway sub devices sub_devices = gateway.devices coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR] for sub_device in sub_devices.values(): for sensor, description in SENSOR_TYPES.items(): if sensor not in sub_device.status: continue entities.append( XiaomiGatewaySensor(coordinator, sub_device, config_entry, description)) elif config_entry.data[CONF_FLOW_TYPE] == CONF_DEVICE: host = config_entry.data[CONF_HOST] token = config_entry.data[CONF_TOKEN] model = config_entry.data[CONF_MODEL] device = hass.data[DOMAIN][config_entry.entry_id].get(KEY_DEVICE) sensors = [] if model in (MODEL_FAN_ZA1, MODEL_FAN_ZA3, MODEL_FAN_ZA4, MODEL_FAN_P5): return if model in MODEL_TO_SENSORS_MAP: sensors = MODEL_TO_SENSORS_MAP[model] elif model in MODELS_HUMIDIFIER_MIOT: sensors = HUMIDIFIER_MIOT_SENSORS elif model in MODELS_HUMIDIFIER_MJJSQ: sensors = HUMIDIFIER_MJJSQ_SENSORS elif model in MODELS_HUMIDIFIER_MIIO: sensors = HUMIDIFIER_MIIO_SENSORS elif model in MODELS_PURIFIER_MIIO: sensors = PURIFIER_MIIO_SENSORS elif model in MODELS_PURIFIER_MIOT: sensors = PURIFIER_MIOT_SENSORS else: unique_id = config_entry.unique_id name = config_entry.title _LOGGER.debug("Initializing with host %s (token %s...)", host, token[:5]) device = AirQualityMonitor(host, token) description = SENSOR_TYPES[ATTR_AIR_QUALITY] entities.append( XiaomiAirQualityMonitor(name, device, config_entry, unique_id, description)) for sensor, description in SENSOR_TYPES.items(): if sensor not in sensors: continue entities.append( XiaomiGenericSensor( f"{config_entry.title} {description.name}", device, config_entry, f"{sensor}_{config_entry.unique_id}", hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR], description, )) async_add_entities(entities)
async def async_setup_entry(hass, config_entry, async_add_entities): """Set up the Xiaomi sensor from a config entry.""" entities = [] if config_entry.data[CONF_FLOW_TYPE] == CONF_GATEWAY: gateway = hass.data[DOMAIN][config_entry.entry_id][CONF_GATEWAY] # Gateway illuminance sensor if gateway.model not in [ GATEWAY_MODEL_AC_V1, GATEWAY_MODEL_AC_V2, GATEWAY_MODEL_AC_V3, GATEWAY_MODEL_EU, ]: entities.append( XiaomiGatewayIlluminanceSensor(gateway, config_entry.title, config_entry.unique_id)) # Gateway sub devices sub_devices = gateway.devices coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR] for sub_device in sub_devices.values(): sensor_variables = set(sub_device.status) & set(SENSOR_TYPES) if sensor_variables: entities.extend([ XiaomiGatewaySensor(coordinator, sub_device, config_entry, variable) for variable in sensor_variables ]) elif config_entry.data[CONF_FLOW_TYPE] == CONF_DEVICE: host = config_entry.data[CONF_HOST] token = config_entry.data[CONF_TOKEN] model = config_entry.data[CONF_MODEL] device = None sensors = [] if model in MODELS_HUMIDIFIER_MIOT: device = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE] coordinator = hass.data[DOMAIN][ config_entry.entry_id][KEY_COORDINATOR] sensors = HUMIDIFIER_SENSORS_MIOT elif model.startswith("zhimi.humidifier."): device = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE] coordinator = hass.data[DOMAIN][ config_entry.entry_id][KEY_COORDINATOR] sensors = HUMIDIFIER_SENSORS else: unique_id = config_entry.unique_id name = config_entry.title _LOGGER.debug("Initializing with host %s (token %s...)", host, token[:5]) device = AirQualityMonitor(host, token) entities.append( XiaomiAirQualityMonitor(name, device, config_entry, unique_id)) for sensor in sensors: entities.append( XiaomiGenericSensor( f"{config_entry.title} {sensor.replace('_', ' ').title()}", device, config_entry, f"{sensor}_{config_entry.unique_id}", sensor, hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR], )) async_add_entities(entities)
async def async_setup_entry( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up the Xiaomi sensor from a config entry.""" entities: list[SensorEntity] = [] if config_entry.data[CONF_FLOW_TYPE] == CONF_GATEWAY: gateway = hass.data[DOMAIN][config_entry.entry_id][CONF_GATEWAY] # Gateway illuminance sensor if gateway.model not in [ GATEWAY_MODEL_AC_V1, GATEWAY_MODEL_AC_V2, GATEWAY_MODEL_AC_V3, GATEWAY_MODEL_AQARA, GATEWAY_MODEL_EU, ]: description = SENSOR_TYPES[ATTR_ILLUMINANCE] entities.append( XiaomiGatewayIlluminanceSensor( gateway, config_entry.title, config_entry.unique_id, description ) ) # Gateway sub devices sub_devices = gateway.devices for sub_device in sub_devices.values(): coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR][ sub_device.sid ] for sensor, description in SENSOR_TYPES.items(): if sensor not in sub_device.status: continue entities.append( XiaomiGatewaySensor( coordinator, sub_device, config_entry, description ) ) elif config_entry.data[CONF_FLOW_TYPE] == CONF_DEVICE: host = config_entry.data[CONF_HOST] token = config_entry.data[CONF_TOKEN] model: str = config_entry.data[CONF_MODEL] if model in (MODEL_FAN_ZA1, MODEL_FAN_ZA3, MODEL_FAN_ZA4, MODEL_FAN_P5): return if model in MODELS_AIR_QUALITY_MONITOR: unique_id = config_entry.unique_id name = config_entry.title _LOGGER.debug("Initializing with host %s (token %s...)", host, token[:5]) device = AirQualityMonitor(host, token) description = SENSOR_TYPES[ATTR_AIR_QUALITY] entities.append( XiaomiAirQualityMonitor( name, device, config_entry, unique_id, description ) ) else: device = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE] sensors: Iterable[str] = [] if model in MODEL_TO_SENSORS_MAP: sensors = MODEL_TO_SENSORS_MAP[model] elif model in MODELS_HUMIDIFIER_MIOT: sensors = HUMIDIFIER_MIOT_SENSORS elif model in MODELS_HUMIDIFIER_MJJSQ: sensors = HUMIDIFIER_MJJSQ_SENSORS elif model in MODELS_HUMIDIFIER_MIIO: sensors = HUMIDIFIER_MIIO_SENSORS elif model in MODELS_PURIFIER_MIIO: sensors = PURIFIER_MIIO_SENSORS elif model in MODELS_PURIFIER_MIOT: sensors = PURIFIER_MIOT_SENSORS elif ( model in MODELS_VACUUM or model.startswith(ROBOROCK_GENERIC) or model.startswith(ROCKROBO_GENERIC) ): return _setup_vacuum_sensors(hass, config_entry, async_add_entities) for sensor, description in SENSOR_TYPES.items(): if sensor not in sensors: continue entities.append( XiaomiGenericSensor( device, config_entry, f"{sensor}_{config_entry.unique_id}", hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR], description, ) ) async_add_entities(entities)