From 886dc355f930ea2dc2e3414ed283ff6a7d61ac5e Mon Sep 17 00:00:00 2001 From: cnrd Date: Tue, 24 Jul 2018 00:47:22 +0200 Subject: [PATCH 1/6] Add support for states --- .../components/vacuum/xiaomi_miio.py | 66 ++++++++++--------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/vacuum/xiaomi_miio.py b/homeassistant/components/vacuum/xiaomi_miio.py index f6789d78b9ae9..ccb55b909b6e6 100644 --- a/homeassistant/components/vacuum/xiaomi_miio.py +++ b/homeassistant/components/vacuum/xiaomi_miio.py @@ -13,8 +13,10 @@ from homeassistant.components.vacuum import ( ATTR_CLEANED_AREA, DOMAIN, PLATFORM_SCHEMA, SUPPORT_BATTERY, SUPPORT_CLEAN_SPOT, SUPPORT_FAN_SPEED, SUPPORT_LOCATE, SUPPORT_PAUSE, - SUPPORT_RETURN_HOME, SUPPORT_SEND_COMMAND, SUPPORT_STATUS, SUPPORT_STOP, - SUPPORT_TURN_OFF, SUPPORT_TURN_ON, VACUUM_SERVICE_SCHEMA, VacuumDevice) + SUPPORT_RETURN_HOME, SUPPORT_SEND_COMMAND, SUPPORT_STOP, + SUPPORT_STATE, VACUUM_SERVICE_SCHEMA, VacuumDevice, + STATE_CLEANING, STATE_DOCKED, STATE_PAUSED, STATE_IDLE, STATE_RETURNING, + STATE_ERROR) from homeassistant.const import ( ATTR_ENTITY_ID, CONF_HOST, CONF_NAME, CONF_TOKEN, STATE_OFF, STATE_ON) import homeassistant.helpers.config_validation as cv @@ -77,11 +79,25 @@ 'schema': SERVICE_SCHEMA_REMOTE_CONTROL}, } -SUPPORT_XIAOMI = SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_PAUSE | \ +SUPPORT_XIAOMI = SUPPORT_STATE | SUPPORT_PAUSE | \ SUPPORT_STOP | SUPPORT_RETURN_HOME | SUPPORT_FAN_SPEED | \ SUPPORT_SEND_COMMAND | SUPPORT_LOCATE | \ - SUPPORT_STATUS | SUPPORT_BATTERY | SUPPORT_CLEAN_SPOT - + SUPPORT_BATTERY | SUPPORT_CLEAN_SPOT + + +STATE_CODE_TO_STATE = { + 3: STATE_IDLE, + 5: STATE_CLEANING, + 6: STATE_RETURNING, + 8: STATE_DOCKED, + 9: STATE_ERROR, + 10: STATE_PAUSED, + 11: STATE_CLEANING, + 12: STATE_ERROR, + 15: STATE_RETURNING, + 16: STATE_CLEANING, + 17: STATE_CLEANING, +} @asyncio.coroutine def async_setup_platform(hass, config, async_add_devices, discovery_info=None): @@ -144,7 +160,6 @@ def __init__(self, name, vacuum): self._vacuum = vacuum self.vacuum_state = None - self._is_on = False self._available = False self.consumable_state = None @@ -157,10 +172,16 @@ def name(self): return self._name @property - def status(self): + def state(self): """Return the status of the vacuum cleaner.""" if self.vacuum_state is not None: - return self.vacuum_state.state + try: + return STATE_CODE_TO_STATE[int(self.vacuum_state.state_code)] + except KeyError: + _LOGGER.error("STATE not supported: %s, state_code: %s, please report", + self.vacuum_state.state, + self.vacuum_state.state_code) + return None @property def battery_level(self): @@ -217,11 +238,6 @@ def device_state_attributes(self): attrs[ATTR_ERROR] = self.vacuum_state.error return attrs - @property - def is_on(self) -> bool: - """Return True if entity is on.""" - return self._is_on - @property def available(self) -> bool: """Return True if entity is available.""" @@ -244,25 +260,16 @@ def _try_command(self, mask_error, func, *args, **kwargs): return False @asyncio.coroutine - def async_turn_on(self, **kwargs): + def async_start(self, **kwargs): """Turn the vacuum on.""" - is_on = yield from self._try_command( + self._try_command( "Unable to start the vacuum: %s", self._vacuum.start) - self._is_on = is_on - - @asyncio.coroutine - def async_turn_off(self, **kwargs): - """Turn the vacuum off and return to home.""" - yield from self.async_stop() - yield from self.async_return_to_base() @asyncio.coroutine def async_stop(self, **kwargs): """Stop the vacuum cleaner.""" - stopped = yield from self._try_command( + self._try_command( "Unable to stop: %s", self._vacuum.stop) - if stopped: - self._is_on = False @asyncio.coroutine def async_set_fan_speed(self, fan_speed, **kwargs): @@ -284,19 +291,17 @@ def async_set_fan_speed(self, fan_speed, **kwargs): @asyncio.coroutine def async_start_pause(self, **kwargs): """Start, pause or resume the cleaning task.""" - if self.vacuum_state and self.is_on: + if self.vacuum_state and self.state == STATE_CLEANING: yield from self._try_command( "Unable to set start/pause: %s", self._vacuum.pause) else: - yield from self.async_turn_on() + yield from self.async_start() @asyncio.coroutine def async_return_to_base(self, **kwargs): """Set the vacuum cleaner to return to the dock.""" - return_home = yield from self._try_command( + self._try_command( "Unable to return home: %s", self._vacuum.home) - if return_home: - self._is_on = False @asyncio.coroutine def async_clean_spot(self, **kwargs): @@ -365,7 +370,6 @@ def update(self): self.clean_history = self._vacuum.clean_history() self.dnd_state = self._vacuum.dnd_status() - self._is_on = state.is_on self._available = True except OSError as exc: _LOGGER.error("Got OSError while fetching the state: %s", exc) From 6a0082980bfbd245c005a37c09588a70d9c46b9b Mon Sep 17 00:00:00 2001 From: cnrd Date: Tue, 24 Jul 2018 00:51:15 +0200 Subject: [PATCH 2/6] Woof? --- homeassistant/components/vacuum/xiaomi_miio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/vacuum/xiaomi_miio.py b/homeassistant/components/vacuum/xiaomi_miio.py index ccb55b909b6e6..ecdce1c131a96 100644 --- a/homeassistant/components/vacuum/xiaomi_miio.py +++ b/homeassistant/components/vacuum/xiaomi_miio.py @@ -178,7 +178,7 @@ def state(self): try: return STATE_CODE_TO_STATE[int(self.vacuum_state.state_code)] except KeyError: - _LOGGER.error("STATE not supported: %s, state_code: %s, please report", + _LOGGER.error("STATE not supported: %s, state_code: %s", self.vacuum_state.state, self.vacuum_state.state_code) return None From 2f93abcf2fb3df099e4597c0c5b244b370c9248c Mon Sep 17 00:00:00 2001 From: cnrd Date: Wed, 25 Jul 2018 01:40:53 +0200 Subject: [PATCH 3/6] Fixed some errors --- homeassistant/components/vacuum/xiaomi_miio.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/vacuum/xiaomi_miio.py b/homeassistant/components/vacuum/xiaomi_miio.py index ecdce1c131a96..867151599503e 100644 --- a/homeassistant/components/vacuum/xiaomi_miio.py +++ b/homeassistant/components/vacuum/xiaomi_miio.py @@ -99,6 +99,7 @@ 17: STATE_CLEANING, } + @asyncio.coroutine def async_setup_platform(hass, config, async_add_devices, discovery_info=None): """Set up the Xiaomi vacuum cleaner robot platform.""" @@ -262,13 +263,13 @@ def _try_command(self, mask_error, func, *args, **kwargs): @asyncio.coroutine def async_start(self, **kwargs): """Turn the vacuum on.""" - self._try_command( + yield from self._try_command( "Unable to start the vacuum: %s", self._vacuum.start) @asyncio.coroutine def async_stop(self, **kwargs): """Stop the vacuum cleaner.""" - self._try_command( + yield from self._try_command( "Unable to stop: %s", self._vacuum.stop) @asyncio.coroutine @@ -300,7 +301,7 @@ def async_start_pause(self, **kwargs): @asyncio.coroutine def async_return_to_base(self, **kwargs): """Set the vacuum cleaner to return to the dock.""" - self._try_command( + yield from self._try_command( "Unable to return home: %s", self._vacuum.home) @asyncio.coroutine From 507438b5a45c9eb27922b9243a81340bc39cd773 Mon Sep 17 00:00:00 2001 From: cnrd Date: Wed, 25 Jul 2018 01:45:10 +0200 Subject: [PATCH 4/6] VacuumDevice -> StateVacuumDevice --- homeassistant/components/vacuum/xiaomi_miio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/vacuum/xiaomi_miio.py b/homeassistant/components/vacuum/xiaomi_miio.py index 867151599503e..78234add383e5 100644 --- a/homeassistant/components/vacuum/xiaomi_miio.py +++ b/homeassistant/components/vacuum/xiaomi_miio.py @@ -14,7 +14,7 @@ ATTR_CLEANED_AREA, DOMAIN, PLATFORM_SCHEMA, SUPPORT_BATTERY, SUPPORT_CLEAN_SPOT, SUPPORT_FAN_SPEED, SUPPORT_LOCATE, SUPPORT_PAUSE, SUPPORT_RETURN_HOME, SUPPORT_SEND_COMMAND, SUPPORT_STOP, - SUPPORT_STATE, VACUUM_SERVICE_SCHEMA, VacuumDevice, + SUPPORT_STATE, VACUUM_SERVICE_SCHEMA, StateVacuumDevice, STATE_CLEANING, STATE_DOCKED, STATE_PAUSED, STATE_IDLE, STATE_RETURNING, STATE_ERROR) from homeassistant.const import ( From fb78f5d4ec31c2d92eb53a6fa5bfac1214791e79 Mon Sep 17 00:00:00 2001 From: cnrd Date: Wed, 25 Jul 2018 01:54:04 +0200 Subject: [PATCH 5/6] VacuumDevice -> StateVacuumDevice --- homeassistant/components/vacuum/xiaomi_miio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/vacuum/xiaomi_miio.py b/homeassistant/components/vacuum/xiaomi_miio.py index 78234add383e5..40a5babf95572 100644 --- a/homeassistant/components/vacuum/xiaomi_miio.py +++ b/homeassistant/components/vacuum/xiaomi_miio.py @@ -152,7 +152,7 @@ def async_service_handler(service): schema=schema) -class MiroboVacuum(VacuumDevice): +class MiroboVacuum(StateVacuumDevice): """Representation of a Xiaomi Vacuum cleaner robot.""" def __init__(self, name, vacuum): From 106bbde89f16e0834fa58b4863b9b6bad6f0f3d3 Mon Sep 17 00:00:00 2001 From: cnrd Date: Fri, 3 Aug 2018 11:15:19 +0200 Subject: [PATCH 6/6] Added split of start and pause --- .../components/vacuum/xiaomi_miio.py | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/vacuum/xiaomi_miio.py b/homeassistant/components/vacuum/xiaomi_miio.py index 40a5babf95572..2c6057e1cf694 100644 --- a/homeassistant/components/vacuum/xiaomi_miio.py +++ b/homeassistant/components/vacuum/xiaomi_miio.py @@ -14,7 +14,7 @@ ATTR_CLEANED_AREA, DOMAIN, PLATFORM_SCHEMA, SUPPORT_BATTERY, SUPPORT_CLEAN_SPOT, SUPPORT_FAN_SPEED, SUPPORT_LOCATE, SUPPORT_PAUSE, SUPPORT_RETURN_HOME, SUPPORT_SEND_COMMAND, SUPPORT_STOP, - SUPPORT_STATE, VACUUM_SERVICE_SCHEMA, StateVacuumDevice, + SUPPORT_STATE, SUPPORT_START, VACUUM_SERVICE_SCHEMA, StateVacuumDevice, STATE_CLEANING, STATE_DOCKED, STATE_PAUSED, STATE_IDLE, STATE_RETURNING, STATE_ERROR) from homeassistant.const import ( @@ -82,7 +82,7 @@ SUPPORT_XIAOMI = SUPPORT_STATE | SUPPORT_PAUSE | \ SUPPORT_STOP | SUPPORT_RETURN_HOME | SUPPORT_FAN_SPEED | \ SUPPORT_SEND_COMMAND | SUPPORT_LOCATE | \ - SUPPORT_BATTERY | SUPPORT_CLEAN_SPOT + SUPPORT_BATTERY | SUPPORT_CLEAN_SPOT | SUPPORT_START STATE_CODE_TO_STATE = { @@ -260,12 +260,17 @@ def _try_command(self, mask_error, func, *args, **kwargs): _LOGGER.error(mask_error, exc) return False - @asyncio.coroutine - def async_start(self, **kwargs): - """Turn the vacuum on.""" - yield from self._try_command( + async def async_start(self): + """Start or resume the cleaning task.""" + await self._try_command( "Unable to start the vacuum: %s", self._vacuum.start) + async def async_pause(self): + """Pause the cleaning task.""" + if self.state == STATE_CLEANING: + await self._try_command( + "Unable to set start/pause: %s", self._vacuum.pause) + @asyncio.coroutine def async_stop(self, **kwargs): """Stop the vacuum cleaner.""" @@ -289,15 +294,6 @@ def async_set_fan_speed(self, fan_speed, **kwargs): "Unable to set fan speed: %s", self._vacuum.set_fan_speed, fan_speed) - @asyncio.coroutine - def async_start_pause(self, **kwargs): - """Start, pause or resume the cleaning task.""" - if self.vacuum_state and self.state == STATE_CLEANING: - yield from self._try_command( - "Unable to set start/pause: %s", self._vacuum.pause) - else: - yield from self.async_start() - @asyncio.coroutine def async_return_to_base(self, **kwargs): """Set the vacuum cleaner to return to the dock."""