Refactoring.

This commit is contained in:
Sebastian Muszynski
2018-03-16 09:59:25 +01:00
parent d3664113e6
commit 4703e15a00

View File

@@ -190,46 +190,45 @@ OPERATION_MODES_AIRPURIFIER_V3 = ['Auto', 'Silent', 'Favorite', 'Idle',
SUCCESS = ['ok'] SUCCESS = ['ok']
SUPPORT_SET_BUZZER = 8 FEATURE_SET_BUZZER = 1
SUPPORT_SET_LED = 16 FEATURE_SET_LED = 2
SUPPORT_SET_CHILD_LOCK = 32 FEATURE_SET_CHILD_LOCK = 4
SUPPORT_SET_LED_BRIGHTNESS = 64 FEATURE_SET_LED_BRIGHTNESS = 8
SUPPORT_SET_FAVORITE_LEVEL = 128 FEATURE_SET_FAVORITE_LEVEL = 16
SUPPORT_SET_AUTO_DETECT = 256 FEATURE_SET_AUTO_DETECT = 32
SUPPORT_SET_LEARN_MODE = 512 FEATURE_SET_LEARN_MODE = 64
SUPPORT_SET_VOLUME = 1024 FEATURE_SET_VOLUME = 128
SUPPORT_RESET_FILTER = 2048 FEATURE_RESET_FILTER = 256
SUPPORT_SET_EXTRA_FEATURES = 4096 FEATURE_SET_EXTRA_FEATURES = 512
SUPPORT_SET_TARGET_HUMIDITY = 8192 FEATURE_SET_TARGET_HUMIDITY = 1024
SUPPORT_SET_DRY = 16384 FEATURE_SET_DRY = 2048
SUPPORT_FLAGS_GENERIC = (SUPPORT_SET_SPEED | FEATURE_FLAGS_GENERIC = (FEATURE_SET_BUZZER |
SUPPORT_SET_BUZZER | FEATURE_SET_CHILD_LOCK)
SUPPORT_SET_CHILD_LOCK)
SUPPORT_FLAGS_AIRPURIFIER = (SUPPORT_FLAGS_GENERIC | FEATURE_FLAGS_AIRPURIFIER = (FEATURE_FLAGS_GENERIC |
SUPPORT_SET_LED | FEATURE_SET_LED |
SUPPORT_SET_LED_BRIGHTNESS | FEATURE_SET_LED_BRIGHTNESS |
SUPPORT_SET_FAVORITE_LEVEL | FEATURE_SET_FAVORITE_LEVEL |
SUPPORT_SET_LEARN_MODE | FEATURE_SET_LEARN_MODE |
SUPPORT_RESET_FILTER | FEATURE_RESET_FILTER |
SUPPORT_SET_EXTRA_FEATURES) FEATURE_SET_EXTRA_FEATURES)
SUPPORT_FLAGS_AIRPURIFIER_PRO = (SUPPORT_SET_SPEED | FEATURE_FLAGS_AIRPURIFIER_PRO = (FEATURE_SET_CHILD_LOCK |
SUPPORT_SET_CHILD_LOCK | FEATURE_SET_LED |
SUPPORT_SET_LED | FEATURE_SET_FAVORITE_LEVEL |
SUPPORT_SET_FAVORITE_LEVEL | FEATURE_SET_AUTO_DETECT |
SUPPORT_SET_AUTO_DETECT | FEATURE_SET_VOLUME)
SUPPORT_SET_VOLUME)
SUPPORT_FLAGS_AIRPURIFIER_V3 = (SUPPORT_FLAGS_GENERIC | SUPPORT_SET_LED) FEATURE_FLAGS_AIRPURIFIER_V3 = (FEATURE_FLAGS_GENERIC |
FEATURE_SET_LED)
SUPPORT_FLAGS_AIRHUMIDIFIER = (SUPPORT_FLAGS_GENERIC | FEATURE_FLAGS_AIRHUMIDIFIER = (FEATURE_FLAGS_GENERIC |
SUPPORT_SET_LED_BRIGHTNESS | FEATURE_SET_LED_BRIGHTNESS |
SUPPORT_SET_TARGET_HUMIDITY) FEATURE_SET_TARGET_HUMIDITY)
SUPPORT_FLAGS_AIRHUMIDIFIER_CA = (SUPPORT_FLAGS_AIRHUMIDIFIER | FEATURE_FLAGS_AIRHUMIDIFIER_CA = (FEATURE_FLAGS_AIRHUMIDIFIER |
SUPPORT_SET_DRY) FEATURE_SET_DRY)
SERVICE_SET_BUZZER_ON = 'xiaomi_miio_set_buzzer_on' SERVICE_SET_BUZZER_ON = 'xiaomi_miio_set_buzzer_on'
SERVICE_SET_BUZZER_OFF = 'xiaomi_miio_set_buzzer_off' SERVICE_SET_BUZZER_OFF = 'xiaomi_miio_set_buzzer_off'
@@ -372,6 +371,8 @@ async def async_setup_platform(hass, config, async_add_devices,
update_tasks = [] update_tasks = []
for device in devices: for device in devices:
if not hasattr(device, method['method']):
continue
await getattr(device, method['method'])(**params) await getattr(device, method['method'])(**params)
update_tasks.append(device.async_update_ha_state(True)) update_tasks.append(device.async_update_ha_state(True))
@@ -400,7 +401,7 @@ class XiaomiGenericDevice(FanEntity):
self._state_attrs = { self._state_attrs = {
ATTR_MODEL: self._model, ATTR_MODEL: self._model,
} }
self._additional_supported_features = SUPPORT_FLAGS_GENERIC self._device_features = DEVICE_FEATURE_FLAGS_GENERIC
self._skip_update = False self._skip_update = False
@property @property
@@ -488,7 +489,7 @@ class XiaomiGenericDevice(FanEntity):
async def async_set_buzzer_on(self): async def async_set_buzzer_on(self):
"""Turn the buzzer on.""" """Turn the buzzer on."""
if self._additional_supported_features & SUPPORT_SET_BUZZER == 0: if self._device_features & FEATURE_SET_BUZZER == 0:
return return
await self._try_command( await self._try_command(
@@ -497,7 +498,7 @@ class XiaomiGenericDevice(FanEntity):
async def async_set_buzzer_off(self): async def async_set_buzzer_off(self):
"""Turn the buzzer off.""" """Turn the buzzer off."""
if self._additional_supported_features & SUPPORT_SET_BUZZER == 0: if self._device_features & FEATURE_SET_BUZZER == 0:
return return
await self._try_command( await self._try_command(
@@ -506,7 +507,7 @@ class XiaomiGenericDevice(FanEntity):
async def async_set_child_lock_on(self): async def async_set_child_lock_on(self):
"""Turn the child lock on.""" """Turn the child lock on."""
if self._additional_supported_features & SUPPORT_SET_CHILD_LOCK == 0: if self._device_features & FEATURE_SET_CHILD_LOCK == 0:
return return
await self._try_command( await self._try_command(
@@ -515,48 +516,13 @@ class XiaomiGenericDevice(FanEntity):
async def async_set_child_lock_off(self): async def async_set_child_lock_off(self):
"""Turn the child lock off.""" """Turn the child lock off."""
if self._additional_supported_features & SUPPORT_SET_CHILD_LOCK == 0: if self._device_features & FEATURE_SET_CHILD_LOCK == 0:
return return
await self._try_command( await self._try_command(
"Turning the child lock of the miio device off failed.", "Turning the child lock of the miio device off failed.",
self._device.set_child_lock, False) self._device.set_child_lock, False)
# pylint: disable=no-self-use
async def async_set_led_on(self):
"""Turn the led on."""
return
# pylint: disable=no-self-use
async def async_set_led_off(self):
"""Turn the led off."""
return
# pylint: disable=no-self-use
async def async_set_favorite_level(self, level: int):
"""Set the favorite level."""
return
# pylint: disable=no-self-use
async def async_set_led_brightness(self, brightness: int):
"""Set the led brightness."""
return
# pylint: disable=no-self-use
async def async_set_target_humidity(self, humidity: int):
"""Set the target humidity."""
return
# pylint: disable=no-self-use
async def async_set_dry_on(self):
"""Turn the dry mode on."""
return
# pylint: disable=no-self-use
async def async_set_dry_off(self):
"""Turn the dry mode off."""
return
class XiaomiAirPurifier(XiaomiGenericDevice): class XiaomiAirPurifier(XiaomiGenericDevice):
"""Representation of a Xiaomi Air Purifier.""" """Representation of a Xiaomi Air Purifier."""
@@ -566,15 +532,15 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
XiaomiGenericDevice.__init__(self, name, device, model, unique_id) XiaomiGenericDevice.__init__(self, name, device, model, unique_id)
if self._model == MODEL_AIRPURIFIER_PRO: if self._model == MODEL_AIRPURIFIER_PRO:
self._additional_supported_features = SUPPORT_FLAGS_AIRPURIFIER_PRO self._device_features = FEATURE_FLAGS_AIRPURIFIER_PRO
self._state_attrs.update({attribute: None for attribute in self._state_attrs.update({attribute: None for attribute in
AVAILABLE_ATTRIBUTES_AIRPURIFIER_PRO}) AVAILABLE_ATTRIBUTES_AIRPURIFIER_PRO})
elif self._model == MODEL_AIRPURIFIER_V3: elif self._model == MODEL_AIRPURIFIER_V3:
self._additional_supported_features = SUPPORT_FLAGS_AIRPURIFIER_V3 self._device_features = FEATURE_FLAGS_AIRPURIFIER_V3
self._state_attrs.update({attribute: None for attribute in self._state_attrs.update({attribute: None for attribute in
AVAILABLE_ATTRIBUTES_AIRPURIFIER_V3}) AVAILABLE_ATTRIBUTES_AIRPURIFIER_V3})
else: else:
self._additional_supported_features = SUPPORT_FLAGS_AIRPURIFIER self._device_features = FEATURE_FLAGS_AIRPURIFIER
self._state_attrs.update({attribute: None for attribute in self._state_attrs.update({attribute: None for attribute in
AVAILABLE_ATTRIBUTES_AIRPURIFIER}) AVAILABLE_ATTRIBUTES_AIRPURIFIER})
@@ -650,7 +616,7 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
async def async_set_led_on(self): async def async_set_led_on(self):
"""Turn the led on.""" """Turn the led on."""
if self._additional_supported_features & SUPPORT_SET_LED == 0: if self._device_features & FEATURE_SET_LED == 0:
return return
await self._try_command( await self._try_command(
@@ -659,7 +625,7 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
async def async_set_led_off(self): async def async_set_led_off(self):
"""Turn the led off.""" """Turn the led off."""
if self._additional_supported_features & SUPPORT_SET_LED == 0: if self._device_features & FEATURE_SET_LED == 0:
return return
await self._try_command( await self._try_command(
@@ -668,8 +634,7 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
async def async_set_led_brightness(self, brightness: int = 2): async def async_set_led_brightness(self, brightness: int = 2):
"""Set the led brightness.""" """Set the led brightness."""
if self._additional_supported_features & \ if self._device_features & FEATURE_SET_LED_BRIGHTNESS == 0:
SUPPORT_SET_LED_BRIGHTNESS == 0:
return return
from miio.airpurifier import LedBrightness from miio.airpurifier import LedBrightness
@@ -680,8 +645,7 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
async def async_set_favorite_level(self, level: int = 1): async def async_set_favorite_level(self, level: int = 1):
"""Set the favorite level.""" """Set the favorite level."""
if self._additional_supported_features & \ if self._device_features & FEATURE_SET_FAVORITE_LEVEL == 0:
SUPPORT_SET_FAVORITE_LEVEL == 0:
return return
await self._try_command( await self._try_command(
@@ -690,7 +654,7 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
async def async_set_auto_detect_on(self): async def async_set_auto_detect_on(self):
"""Turn the auto detect on.""" """Turn the auto detect on."""
if self._additional_supported_features & SUPPORT_SET_AUTO_DETECT == 0: if self._device_features & FEATURE_SET_AUTO_DETECT == 0:
return return
await self._try_command( await self._try_command(
@@ -699,7 +663,7 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
async def async_set_auto_detect_off(self): async def async_set_auto_detect_off(self):
"""Turn the auto detect off.""" """Turn the auto detect off."""
if self._additional_supported_features & SUPPORT_SET_AUTO_DETECT == 0: if self._device_features & FEATURE_SET_AUTO_DETECT == 0:
return return
await self._try_command( await self._try_command(
@@ -708,7 +672,7 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
async def async_set_learn_mode_on(self): async def async_set_learn_mode_on(self):
"""Turn the learn mode on.""" """Turn the learn mode on."""
if self._additional_supported_features & SUPPORT_SET_LEARN_MODE == 0: if self._device_features & FEATURE_SET_LEARN_MODE == 0:
return return
await self._try_command( await self._try_command(
@@ -717,7 +681,7 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
async def async_set_learn_mode_off(self): async def async_set_learn_mode_off(self):
"""Turn the learn mode off.""" """Turn the learn mode off."""
if self._additional_supported_features & SUPPORT_SET_LEARN_MODE == 0: if self._device_features & FEATURE_SET_LEARN_MODE == 0:
return return
await self._try_command( await self._try_command(
@@ -726,7 +690,7 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
async def async_set_volume(self, volume: int = 50): async def async_set_volume(self, volume: int = 50):
"""Set the sound volume.""" """Set the sound volume."""
if self._additional_supported_features & SUPPORT_SET_VOLUME == 0: if self._device_features & FEATURE_SET_VOLUME == 0:
return return
await self._try_command( await self._try_command(
@@ -735,8 +699,7 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
async def async_set_extra_features(self, features: int = 1): async def async_set_extra_features(self, features: int = 1):
"""Set the extra features.""" """Set the extra features."""
if self._additional_supported_features & \ if self._device_features & FEATURE_SET_EXTRA_FEATURES == 0:
SUPPORT_SET_EXTRA_FEATURES == 0:
return return
await self._try_command( await self._try_command(
@@ -745,7 +708,7 @@ class XiaomiAirPurifier(XiaomiGenericDevice):
async def async_reset_filter(self): async def async_reset_filter(self):
"""Reset the filter lifetime and usage.""" """Reset the filter lifetime and usage."""
if self._additional_supported_features & SUPPORT_RESET_FILTER == 0: if self._device_features & FEATURE_RESET_FILTER == 0:
return return
await self._try_command( await self._try_command(
@@ -760,7 +723,7 @@ class XiaomiAirHumidifier(XiaomiGenericDevice):
"""Initialize the plug switch.""" """Initialize the plug switch."""
XiaomiGenericDevice.__init__(self, name, device, model, unique_id) XiaomiGenericDevice.__init__(self, name, device, model, unique_id)
self._additional_supported_features = SUPPORT_FLAGS_AIRHUMIDIFIER self._device_features = FEATURE_FLAGS_AIRHUMIDIFIER
self._state_attrs.update({attribute: None for attribute in self._state_attrs.update({attribute: None for attribute in
AVAILABLE_ATTRIBUTES_AIRHUMIDIFIER}) AVAILABLE_ATTRIBUTES_AIRHUMIDIFIER})
@@ -822,8 +785,7 @@ class XiaomiAirHumidifier(XiaomiGenericDevice):
async def async_set_led_brightness(self, brightness: int = 2): async def async_set_led_brightness(self, brightness: int = 2):
"""Set the led brightness.""" """Set the led brightness."""
if self._additional_supported_features & \ if self._device_features & FEATURE_SET_LED_BRIGHTNESS == 0:
SUPPORT_SET_LED_BRIGHTNESS == 0:
return return
from miio.airhumidifier import LedBrightness from miio.airhumidifier import LedBrightness
@@ -834,8 +796,7 @@ class XiaomiAirHumidifier(XiaomiGenericDevice):
async def async_set_target_humidity(self, humidity: int = 40): async def async_set_target_humidity(self, humidity: int = 40):
"""Set the target humidity.""" """Set the target humidity."""
if self._additional_supported_features & \ if self._device_features & FEATURE_SET_TARGET_HUMIDITY == 0:
SUPPORT_SET_TARGET_HUMIDITY == 0:
return return
await self._try_command( await self._try_command(
@@ -844,7 +805,7 @@ class XiaomiAirHumidifier(XiaomiGenericDevice):
async def async_set_dry_on(self): async def async_set_dry_on(self):
"""Turn the dry mode on.""" """Turn the dry mode on."""
if self._additional_supported_features & SUPPORT_SET_DRY == 0: if self._device_features & FEATURE_SET_DRY == 0:
return return
await self._try_command( await self._try_command(
@@ -853,7 +814,7 @@ class XiaomiAirHumidifier(XiaomiGenericDevice):
async def async_set_dry_off(self): async def async_set_dry_off(self):
"""Turn the dry mode off.""" """Turn the dry mode off."""
if self._additional_supported_features & SUPPORT_SET_DRY == 0: if self._device_features & FEATURE_SET_DRY == 0:
return return
await self._try_command( await self._try_command(