mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Xiaomi MiIO Switch: Allow unavailable devices at startup by model setting (#12626)
* Unavailable state introduced if the device isn't reachable. * Redundancy removed. * Pylint fixed. * Missing space added. * Pylint fixed. * Use format instead of concatenation.
This commit is contained in:
parent
b0e062b2f8
commit
e7b84432f9
@ -19,10 +19,18 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
DEFAULT_NAME = 'Xiaomi Miio Switch'
|
DEFAULT_NAME = 'Xiaomi Miio Switch'
|
||||||
|
|
||||||
|
CONF_MODEL = 'model'
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_HOST): cv.string,
|
vol.Required(CONF_HOST): cv.string,
|
||||||
vol.Required(CONF_TOKEN): vol.All(cv.string, vol.Length(min=32, max=32)),
|
vol.Required(CONF_TOKEN): vol.All(cv.string, vol.Length(min=32, max=32)),
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
|
vol.Optional(CONF_MODEL): vol.In(
|
||||||
|
['chuangmi.plug.v1',
|
||||||
|
'qmi.powerstrip.v1',
|
||||||
|
'zimi.powerstrip.v2',
|
||||||
|
'chuangmi.plug.m1',
|
||||||
|
'chuangmi.plug.v2']),
|
||||||
})
|
})
|
||||||
|
|
||||||
REQUIREMENTS = ['python-miio==0.3.7']
|
REQUIREMENTS = ['python-miio==0.3.7']
|
||||||
@ -43,19 +51,25 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
host = config.get(CONF_HOST)
|
host = config.get(CONF_HOST)
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
token = config.get(CONF_TOKEN)
|
token = config.get(CONF_TOKEN)
|
||||||
|
model = config.get(CONF_MODEL)
|
||||||
|
|
||||||
_LOGGER.info("Initializing with host %s (token %s...)", host, token[:5])
|
_LOGGER.info("Initializing with host %s (token %s...)", host, token[:5])
|
||||||
|
|
||||||
devices = []
|
devices = []
|
||||||
|
|
||||||
|
if model is None:
|
||||||
try:
|
try:
|
||||||
plug = Device(host, token)
|
miio_device = Device(host, token)
|
||||||
device_info = plug.info()
|
device_info = miio_device.info()
|
||||||
_LOGGER.info("%s %s %s initialized",
|
model = device_info.model
|
||||||
device_info.model,
|
_LOGGER.info("%s %s %s detected",
|
||||||
|
model,
|
||||||
device_info.firmware_version,
|
device_info.firmware_version,
|
||||||
device_info.hardware_version)
|
device_info.hardware_version)
|
||||||
|
except DeviceException:
|
||||||
|
raise PlatformNotReady
|
||||||
|
|
||||||
if device_info.model in ['chuangmi.plug.v1']:
|
if model in ['chuangmi.plug.v1']:
|
||||||
from miio import PlugV1
|
from miio import PlugV1
|
||||||
plug = PlugV1(host, token)
|
plug = PlugV1(host, token)
|
||||||
|
|
||||||
@ -63,28 +77,27 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
# A switch device per channel will be created.
|
# A switch device per channel will be created.
|
||||||
for channel_usb in [True, False]:
|
for channel_usb in [True, False]:
|
||||||
device = ChuangMiPlugV1Switch(
|
device = ChuangMiPlugV1Switch(
|
||||||
name, plug, device_info, channel_usb)
|
name, plug, model, channel_usb)
|
||||||
devices.append(device)
|
devices.append(device)
|
||||||
|
|
||||||
elif device_info.model in ['qmi.powerstrip.v1',
|
elif model in ['qmi.powerstrip.v1',
|
||||||
'zimi.powerstrip.v2']:
|
'zimi.powerstrip.v2']:
|
||||||
from miio import PowerStrip
|
from miio import PowerStrip
|
||||||
plug = PowerStrip(host, token)
|
plug = PowerStrip(host, token)
|
||||||
device = XiaomiPowerStripSwitch(name, plug, device_info)
|
device = XiaomiPowerStripSwitch(name, plug, model)
|
||||||
devices.append(device)
|
devices.append(device)
|
||||||
elif device_info.model in ['chuangmi.plug.m1',
|
elif model in ['chuangmi.plug.m1',
|
||||||
'chuangmi.plug.v2']:
|
'chuangmi.plug.v2']:
|
||||||
from miio import Plug
|
from miio import Plug
|
||||||
plug = Plug(host, token)
|
plug = Plug(host, token)
|
||||||
device = XiaomiPlugGenericSwitch(name, plug, device_info)
|
device = XiaomiPlugGenericSwitch(name, plug, model)
|
||||||
devices.append(device)
|
devices.append(device)
|
||||||
else:
|
else:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
'Unsupported device found! Please create an issue at '
|
'Unsupported device found! Please create an issue at '
|
||||||
'https://github.com/rytilahti/python-miio/issues '
|
'https://github.com/rytilahti/python-miio/issues '
|
||||||
'and provide the following data: %s', device_info.model)
|
'and provide the following data: %s', model)
|
||||||
except DeviceException:
|
return False
|
||||||
raise PlatformNotReady
|
|
||||||
|
|
||||||
async_add_devices(devices, update_before_add=True)
|
async_add_devices(devices, update_before_add=True)
|
||||||
|
|
||||||
@ -92,17 +105,17 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
class XiaomiPlugGenericSwitch(SwitchDevice):
|
class XiaomiPlugGenericSwitch(SwitchDevice):
|
||||||
"""Representation of a Xiaomi Plug Generic."""
|
"""Representation of a Xiaomi Plug Generic."""
|
||||||
|
|
||||||
def __init__(self, name, plug, device_info):
|
def __init__(self, name, plug, model):
|
||||||
"""Initialize the plug switch."""
|
"""Initialize the plug switch."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._icon = 'mdi:power-socket'
|
self._icon = 'mdi:power-socket'
|
||||||
self._device_info = device_info
|
self._model = model
|
||||||
|
|
||||||
self._plug = plug
|
self._plug = plug
|
||||||
self._state = None
|
self._state = None
|
||||||
self._state_attrs = {
|
self._state_attrs = {
|
||||||
ATTR_TEMPERATURE: None,
|
ATTR_TEMPERATURE: None,
|
||||||
ATTR_MODEL: self._device_info.model,
|
ATTR_MODEL: self._model,
|
||||||
}
|
}
|
||||||
self._skip_update = False
|
self._skip_update = False
|
||||||
|
|
||||||
@ -191,20 +204,21 @@ class XiaomiPlugGenericSwitch(SwitchDevice):
|
|||||||
})
|
})
|
||||||
|
|
||||||
except DeviceException as ex:
|
except DeviceException as ex:
|
||||||
|
self._state = None
|
||||||
_LOGGER.error("Got exception while fetching the state: %s", ex)
|
_LOGGER.error("Got exception while fetching the state: %s", ex)
|
||||||
|
|
||||||
|
|
||||||
class XiaomiPowerStripSwitch(XiaomiPlugGenericSwitch, SwitchDevice):
|
class XiaomiPowerStripSwitch(XiaomiPlugGenericSwitch, SwitchDevice):
|
||||||
"""Representation of a Xiaomi Power Strip."""
|
"""Representation of a Xiaomi Power Strip."""
|
||||||
|
|
||||||
def __init__(self, name, plug, device_info):
|
def __init__(self, name, plug, model):
|
||||||
"""Initialize the plug switch."""
|
"""Initialize the plug switch."""
|
||||||
XiaomiPlugGenericSwitch.__init__(self, name, plug, device_info)
|
XiaomiPlugGenericSwitch.__init__(self, name, plug, model)
|
||||||
|
|
||||||
self._state_attrs = {
|
self._state_attrs = {
|
||||||
ATTR_TEMPERATURE: None,
|
ATTR_TEMPERATURE: None,
|
||||||
ATTR_LOAD_POWER: None,
|
ATTR_LOAD_POWER: None,
|
||||||
ATTR_MODEL: self._device_info.model,
|
ATTR_MODEL: self._model,
|
||||||
}
|
}
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
@ -228,17 +242,18 @@ class XiaomiPowerStripSwitch(XiaomiPlugGenericSwitch, SwitchDevice):
|
|||||||
})
|
})
|
||||||
|
|
||||||
except DeviceException as ex:
|
except DeviceException as ex:
|
||||||
|
self._state = None
|
||||||
_LOGGER.error("Got exception while fetching the state: %s", ex)
|
_LOGGER.error("Got exception while fetching the state: %s", ex)
|
||||||
|
|
||||||
|
|
||||||
class ChuangMiPlugV1Switch(XiaomiPlugGenericSwitch, SwitchDevice):
|
class ChuangMiPlugV1Switch(XiaomiPlugGenericSwitch, SwitchDevice):
|
||||||
"""Representation of a Chuang Mi Plug V1."""
|
"""Representation of a Chuang Mi Plug V1."""
|
||||||
|
|
||||||
def __init__(self, name, plug, device_info, channel_usb):
|
def __init__(self, name, plug, model, channel_usb):
|
||||||
"""Initialize the plug switch."""
|
"""Initialize the plug switch."""
|
||||||
name = name + ' USB' if channel_usb else name
|
name = '{} USB'.format(name) if channel_usb else name
|
||||||
|
|
||||||
XiaomiPlugGenericSwitch.__init__(self, name, plug, device_info)
|
XiaomiPlugGenericSwitch.__init__(self, name, plug, model)
|
||||||
self._channel_usb = channel_usb
|
self._channel_usb = channel_usb
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
@ -293,4 +308,5 @@ class ChuangMiPlugV1Switch(XiaomiPlugGenericSwitch, SwitchDevice):
|
|||||||
})
|
})
|
||||||
|
|
||||||
except DeviceException as ex:
|
except DeviceException as ex:
|
||||||
|
self._state = None
|
||||||
_LOGGER.error("Got exception while fetching the state: %s", ex)
|
_LOGGER.error("Got exception while fetching the state: %s", ex)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user