def raw_command(dev: miio.Plug, cmd, parameters): """Run a raw command.""" params = [] # type: Any if parameters: params = ast.literal_eval(parameters) click.echo("Sending cmd %s with params %s" % (cmd, params)) click.echo(dev.raw_command(cmd, params))
def status(dev: miio.Plug): """Returns the state information.""" res = dev.status() if not res: return # bail out click.echo(click.style("Power: %s" % res.power, bold=True)) click.echo("Temperature: %s" % res.temperature)
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): """Set up the switch from config.""" from miio import Device, DeviceException host = config.get(CONF_HOST) name = config.get(CONF_NAME) token = config.get(CONF_TOKEN) model = config.get(CONF_MODEL) _LOGGER.info("Initializing with host %s (token %s...)", host, token[:5]) devices = [] if model is None: try: miio_device = Device(host, token) device_info = miio_device.info() model = device_info.model _LOGGER.info("%s %s %s detected", model, device_info.firmware_version, device_info.hardware_version) except DeviceException: raise PlatformNotReady if model in ['chuangmi.plug.v1']: from miio import PlugV1 plug = PlugV1(host, token) # The device has two switchable channels (mains and a USB port). # A switch device per channel will be created. for channel_usb in [True, False]: device = ChuangMiPlugV1Switch( name, plug, model, channel_usb) devices.append(device) elif model in ['qmi.powerstrip.v1', 'zimi.powerstrip.v2']: from miio import PowerStrip plug = PowerStrip(host, token) device = XiaomiPowerStripSwitch(name, plug, model) devices.append(device) elif model in ['chuangmi.plug.m1', 'chuangmi.plug.v2']: from miio import Plug plug = Plug(host, token) device = XiaomiPlugGenericSwitch(name, plug, model) devices.append(device) else: _LOGGER.error( 'Unsupported device found! Please create an issue at ' 'https://github.com/rytilahti/python-miio/issues ' 'and provide the following data: %s', model) return False async_add_devices(devices, update_before_add=True)
def off(dev: miio.Plug): """Power off.""" click.echo("Power off: %s" % dev.off())
def on(dev: miio.Plug): """Power on.""" click.echo("Power on: %s" % dev.on())
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): """Set up the switch from config.""" from miio import Device, DeviceException if PLATFORM not in hass.data: hass.data[PLATFORM] = {} host = config.get(CONF_HOST) name = config.get(CONF_NAME) token = config.get(CONF_TOKEN) model = config.get(CONF_MODEL) _LOGGER.info("Initializing with host %s (token %s...)", host, token[:5]) devices = [] if model is None: try: miio_device = Device(host, token) device_info = miio_device.info() model = device_info.model _LOGGER.info("%s %s %s detected", model, device_info.firmware_version, device_info.hardware_version) except DeviceException: raise PlatformNotReady if model in ['chuangmi.plug.v1']: from miio import PlugV1 plug = PlugV1(host, token) # The device has two switchable channels (mains and a USB port). # A switch device per channel will be created. for channel_usb in [True, False]: device = ChuangMiPlugV1Switch(name, plug, model, channel_usb) devices.append(device) hass.data[PLATFORM][host] = device elif model in ['qmi.powerstrip.v1', 'zimi.powerstrip.v2']: from miio import PowerStrip plug = PowerStrip(host, token) device = XiaomiPowerStripSwitch(name, plug, model) devices.append(device) hass.data[PLATFORM][host] = device elif model in ['chuangmi.plug.m1', 'chuangmi.plug.v2']: from miio import Plug plug = Plug(host, token) device = XiaomiPlugGenericSwitch(name, plug, model) devices.append(device) hass.data[PLATFORM][host] = device else: _LOGGER.error( 'Unsupported device found! Please create an issue at ' 'https://github.com/rytilahti/python-miio/issues ' 'and provide the following data: %s', model) return False async_add_devices(devices, update_before_add=True) @asyncio.coroutine def async_service_handler(service): """Map services to methods on XiaomiPlugGenericSwitch.""" params = { key: value for key, value in service.data.items() if key != ATTR_ENTITY_ID } entity_ids = service.data.get(ATTR_ENTITY_ID) if entity_ids: devices = [ device for device in hass.data[PLATFORM].values() if device.entity_id in entity_ids ] else: devices = hass.data[PLATFORM].values() update_tasks = [] for device in devices: yield from getattr(device, 'async_set_power_mode')(**params) update_tasks.append(device.async_update_ha_state(True)) if update_tasks: yield from asyncio.wait(update_tasks, loop=hass.loop) hass.services.async_register(DOMAIN, SERVICE_SET_POWER_MODE, async_service_handler, schema=SERVICE_SCHEMA_POWER_MODE)