Skip to content

Commit

Permalink
Merge branch 'master' into add-water-purifier-yunmi
Browse files Browse the repository at this point in the history
# Conflicts:
#	custom_components/xiaomi_miot/__init__.py
  • Loading branch information
al-one committed Jan 4, 2021
2 parents 6a27234 + 0d7eb04 commit 193a716
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
26 changes: 16 additions & 10 deletions custom_components/xiaomi_miot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant import core, config_entries
from homeassistant.const import *
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
import homeassistant.helpers.device_registry as dr
import homeassistant.helpers.config_validation as cv
Expand All @@ -31,13 +31,18 @@
DEFAULT_NAME = 'Xiaomi Miot'
CONF_MODEL = 'model'

SUPPORTED_DOMAINS = [
"sensor",
"climate",
]

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
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_MODEL, default=''): cv.string,
vol.Optional(CONF_MODE, default=''): cv.string,
vol.Optional(CONF_MODE, default=[]): cv.ensure_list,
}
)

Expand Down Expand Up @@ -84,18 +89,19 @@ async def async_setup_entry(hass: core.HomeAssistant, config_entry: config_entri
entry_id = config_entry.entry_id
unique_id = config_entry.unique_id
info = config_entry.data.get('miio_info') or {}
platforms = ['sensor', 'climate']
plats = []
config = {}
for k in [CONF_HOST, CONF_TOKEN, CONF_NAME, CONF_MODE, CONF_MODE]:
for k in [CONF_HOST, CONF_TOKEN, CONF_NAME, CONF_MODEL, CONF_MODE]:
config[k] = config_entry.data.get(k)
model = config.get(CONF_MODEL) or info.get(CONF_MODEL) or ''
config[CONF_MODEL] = model
mode = config.get(CONF_MODE) or ''
for m in mode.split(','):
if m in platforms:
modes = config.get(CONF_MODE, [])
if not isinstance(modes, list):
modes = str(modes).split(',')
for m in modes:
if m in SUPPORTED_DOMAINS:
plats.append(m)
config[CONF_MODE] = ''
config[CONF_MODE] = []
if not plats:
if model.find('aircondition') > 0:
plats = ['climate']
Expand All @@ -108,7 +114,7 @@ async def async_setup_entry(hass: core.HomeAssistant, config_entry: config_entri
'entry_id': entry_id,
'unique_id': unique_id,
'config': config,
'plats': plats,
'domains': plats,
'miio': info,
})
for plat in plats:
Expand Down Expand Up @@ -153,7 +159,7 @@ async def async_service_handler(service):
hass.services.async_register(DOMAIN, srv, async_service_handler, schema=schema)


class MiioEntity(ToggleEntity):
class MiioEntity(Entity):
def __init__(self, name, device):
self._device = device
try:
Expand Down
39 changes: 19 additions & 20 deletions custom_components/xiaomi_miot/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,71 @@
DOMAIN,
CONF_MODEL,
DEFAULT_NAME,
SUPPORTED_DOMAINS,
)

_LOGGER = logging.getLogger(__name__)

MIIO_CONFIG_SCHEMA = vol.Schema({
vol.Required(CONF_HOST): str,
vol.Required(CONF_TOKEN): vol.All(str, vol.Length(min=32, max=32)),
vol.Optional(CONF_NAME, default = DEFAULT_NAME): str,
vol.Optional(CONF_MODEL, default = ''): str,
vol.Optional(CONF_MODE, default = ''): str,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): str,
vol.Optional(CONF_MODE, default=[]): cv.multi_select(SUPPORTED_DOMAINS),
})


class XiaomiMiotFlowHandler(config_entries.ConfigFlow, domain = DOMAIN):

class XiaomiMiotFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL

def __init__(self):
self.host = None

async def async_step_user(self, user_input = None):
async def async_step_user(self, user_input=None):
errors = {}
if user_input is not None:
if user_input.get(CONF_HOST):
self.host = user_input[CONF_HOST]
token = user_input.get(CONF_TOKEN)
token = user_input.get(CONF_TOKEN)
device = MiioDevice(self.host, token)
try:
info = device.info()
except:
except DeviceException:
info = None
errors['base'] = 'cannot_connect'
_LOGGER.debug('Xiaomi Miot async_step_user %s', {
'user_input' : user_input,
'info' : info,
'errors' : errors,
'user_input': user_input,
'info': info,
'errors': errors,
})
if info is not None:
unique_id = format_mac(info.mac_address)
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()
user_input['miio_info'] = dict(info.raw or {})
return self.async_create_entry(
title = user_input.get(CONF_NAME),
data = user_input,
title=user_input.get(CONF_NAME),
data=user_input,
)
return self.async_show_form(
step_id = 'user',
data_schema = MIIO_CONFIG_SCHEMA,
errors = errors,
step_id='user',
data_schema=MIIO_CONFIG_SCHEMA,
errors=errors,
)

async def async_step_zeroconf(self, discovery_info):
name = discovery_info.get('name')
self.host = discovery_info.get('host')
mac_address = discovery_info.get('properties', {}).get('mac')
if not name or not self.host or not mac_address:
return self.async_abort(reason = 'not_xiaomi_miio')
return self.async_abort(reason='not_xiaomi_miio')
if not name.startswith('xiaomi'):
_LOGGER.debug('Device %s discovered with host %s, not xiaomi device', name, self.host)
return self.async_abort(reason = 'not_xiaomi_miio')
return self.async_abort(reason='not_xiaomi_miio')
unique_id = format_mac(mac_address)
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured({CONF_HOST : self.host})
self._abort_if_unique_id_configured({CONF_HOST: self.host})
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
self.context.update({
'title_placeholders' : {'name' : f'{name}({self.host})'}
'title_placeholders': {'name': f'{name}({self.host})'}
})
return await self.async_step_user()

0 comments on commit 193a716

Please sign in to comment.