From 941bb57f801badbd2fe59d50cabf957cb78006d0 Mon Sep 17 00:00:00 2001 From: Mick Vleeshouwer Date: Tue, 7 Jul 2020 15:50:26 +0200 Subject: [PATCH] Code readability improvements (#121) Co-authored-by: tetienne --- custom_components/tahoma/binary_sensor.py | 5 +-- custom_components/tahoma/const.py | 2 ++ custom_components/tahoma/cover.py | 1 - custom_components/tahoma/light.py | 42 ++++++++++------------- custom_components/tahoma/lock.py | 10 +++--- custom_components/tahoma/scene.py | 6 +--- custom_components/tahoma/sensor.py | 4 --- custom_components/tahoma/switch.py | 19 ++++------ custom_components/tahoma/tahoma_device.py | 34 ++++++++---------- 9 files changed, 49 insertions(+), 74 deletions(-) diff --git a/custom_components/tahoma/binary_sensor.py b/custom_components/tahoma/binary_sensor.py index 88c88d70f..58e2662e0 100644 --- a/custom_components/tahoma/binary_sensor.py +++ b/custom_components/tahoma/binary_sensor.py @@ -31,9 +31,7 @@ async def async_setup_entry(hass, entry, async_add_entities): """Set up the TaHoma sensors from a config entry.""" - data = hass.data[DOMAIN][entry.entry_id] - controller = data.get("controller") entities = [ @@ -57,7 +55,7 @@ def __init__(self, tahoma_device, controller): @property def is_on(self): """Return the state of the sensor.""" - return bool(self._state == STATE_ON) + return self._state == STATE_ON @property def device_class(self): @@ -71,7 +69,6 @@ def device_class(self): @property def icon(self) -> Optional[str]: """Return the icon to use in the frontend, if any.""" - if self.device_class == DEVICE_CLASS_WATER: if self.is_on: return "mdi:water" diff --git a/custom_components/tahoma/const.py b/custom_components/tahoma/const.py index 4b30c3750..1a89e4002 100644 --- a/custom_components/tahoma/const.py +++ b/custom_components/tahoma/const.py @@ -119,6 +119,7 @@ ATTR_LOCK_ORIG = "lock_originator" # TaHoma internal device states +CORE_BATTERY_STATE = "core:BatteryState" CORE_BLUE_COLOR_INTENSITY_STATE = "core:BlueColorIntensityState" CORE_BUTTON_STATE = "core:ButtonState" CORE_CLOSURE_STATE = "core:ClosureState" @@ -131,6 +132,7 @@ CORE_ELECTRIC_POWER_CONSUMPTION_STATE = "core:ElectricPowerConsumptionState" CORE_GAS_DETECTION_STATE = "core:GasDetectionState" CORE_GREEN_COLOR_INTENSITY_STATE = "core:GreenColorIntensityState" +CORE_LIGHT_INTENSITY_STATE = "core:LightIntensityState" CORE_LUMINANCE_STATE = "core:LuminanceState" CORE_MEASURED_VALUE_TYPE = "core:MeasuredValueType" CORE_MEMORIZED_1_POSITION_STATE = "core:Memorized1PositionState" diff --git a/custom_components/tahoma/cover.py b/custom_components/tahoma/cover.py index 37bcbd6d3..ef6bd0a85 100644 --- a/custom_components/tahoma/cover.py +++ b/custom_components/tahoma/cover.py @@ -50,7 +50,6 @@ async def async_setup_entry(hass, entry, async_add_entities): """Set up the TaHoma covers from a config entry.""" data = hass.data[DOMAIN][entry.entry_id] - controller = data.get("controller") entities = [ diff --git a/custom_components/tahoma/light.py b/custom_components/tahoma/light.py index c6c10c659..cd63c2508 100644 --- a/custom_components/tahoma/light.py +++ b/custom_components/tahoma/light.py @@ -17,6 +17,8 @@ from .const import ( CORE_BLUE_COLOR_INTENSITY_STATE, CORE_GREEN_COLOR_INTENSITY_STATE, + CORE_LIGHT_INTENSITY_STATE, + CORE_ON_OFF_STATE, CORE_RED_COLOR_INTENSITY_STATE, DOMAIN, TAHOMA_TYPES, @@ -32,13 +34,13 @@ async def async_setup_entry(hass, entry, async_add_entities): """Set up the TaHoma lights from a config entry.""" data = hass.data[DOMAIN][entry.entry_id] - - entities = [] controller = data.get("controller") - for device in data.get("devices"): - if TAHOMA_TYPES[device.uiclass] == "light": - entities.append(TahomaLight(device, controller)) + entities = [ + TahomaLight(device, controller) + for device in data.get("devices") + if TAHOMA_TYPES[device.uiclass] == "light" + ] async_add_entities(entities) @@ -70,12 +72,12 @@ def hs_color(self): """Return the hue and saturation color value [float, float].""" if self._hs_color: return self._hs_color + return None @property def supported_features(self) -> int: """Flag supported features.""" - supported_features = 0 if "setIntensity" in self.tahoma_device.command_definitions: @@ -91,8 +93,6 @@ def supported_features(self) -> int: def turn_on(self, **kwargs) -> None: """Turn the light on.""" - self._state = True - if ATTR_HS_COLOR in kwargs: self.apply_action( "setRGB", @@ -101,12 +101,15 @@ def turn_on(self, **kwargs) -> None: for c in color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR]) ], ) + if ATTR_BRIGHTNESS in kwargs: self._brightness = int(float(kwargs[ATTR_BRIGHTNESS]) / 255 * 100) self.apply_action("setIntensity", self._brightness) + elif ATTR_EFFECT in kwargs: self._effect = kwargs[ATTR_EFFECT] self.apply_action("wink", 100) + else: self.apply_action("on") @@ -131,26 +134,19 @@ def effect(self) -> str: def update(self): """Fetch new state data for this light.""" - if self.should_wait(): self.schedule_update_ha_state(True) return self.controller.get_states([self.tahoma_device]) - if "core:LightIntensityState" in self.tahoma_device.active_states: - self._brightness = self.tahoma_device.active_states.get( - "core:LightIntensityState" - ) + states = self.tahoma_device.active_states - if self.tahoma_device.active_states.get("core:OnOffState") == "on": - self._state = True - else: - self._state = False + self._state = states.get(CORE_ON_OFF_STATE) == "on" + self._brightness = states.get(CORE_LIGHT_INTENSITY_STATE) - if CORE_RED_COLOR_INTENSITY_STATE in self.tahoma_device.active_states: - self._hs_color = color_util.color_RGB_to_hs( - self.tahoma_device.active_states.get(CORE_RED_COLOR_INTENSITY_STATE), - self.tahoma_device.active_states.get(CORE_GREEN_COLOR_INTENSITY_STATE), - self.tahoma_device.active_states.get(CORE_BLUE_COLOR_INTENSITY_STATE), - ) + self._hs_color = color_util.color_RGB_to_hs( + states.get(CORE_RED_COLOR_INTENSITY_STATE), + states.get(CORE_GREEN_COLOR_INTENSITY_STATE), + states.get(CORE_BLUE_COLOR_INTENSITY_STATE), + ) diff --git a/custom_components/tahoma/lock.py b/custom_components/tahoma/lock.py index 989b91720..70cd7da75 100644 --- a/custom_components/tahoma/lock.py +++ b/custom_components/tahoma/lock.py @@ -18,13 +18,13 @@ async def async_setup_entry(hass, entry, async_add_entities): """Set up the TaHoma locks from a config entry.""" data = hass.data[DOMAIN][entry.entry_id] - - entities = [] controller = data.get("controller") - for device in data.get("devices"): - if TAHOMA_TYPES[device.uiclass] == "lock": - entities.append(TahomaLock(device, controller)) + entities = [ + TahomaLock(device, controller) + for device in data.get("devices") + if TAHOMA_TYPES[device.uiclass] == "lock" + ] async_add_entities(entities) diff --git a/custom_components/tahoma/scene.py b/custom_components/tahoma/scene.py index 22a42d787..6aaee221e 100644 --- a/custom_components/tahoma/scene.py +++ b/custom_components/tahoma/scene.py @@ -11,14 +11,10 @@ async def async_setup_entry(hass, entry, async_add_entities): """Set up the TaHoma scenes from a config entry.""" - data = hass.data[DOMAIN][entry.entry_id] - - entities = [] controller = data.get("controller") - for scene in data.get("scenes"): - entities.append(TahomaScene(scene, controller)) + entities = [TahomaScene(scene, controller) for scene in data.get("scenes")] async_add_entities(entities) diff --git a/custom_components/tahoma/sensor.py b/custom_components/tahoma/sensor.py index 322b26079..55b8fb587 100644 --- a/custom_components/tahoma/sensor.py +++ b/custom_components/tahoma/sensor.py @@ -43,9 +43,7 @@ async def async_setup_entry(hass, entry, async_add_entities): """Set up the TaHoma sensors from a config entry.""" - data = hass.data[DOMAIN][entry.entry_id] - controller = data.get("controller") entities = [ @@ -74,7 +72,6 @@ def state(self): @property def unit_of_measurement(self): """Return the unit of measurement of this entity, if any.""" - states = self.tahoma_device.active_states if CORE_TEMPERATURE_STATE in states: @@ -111,7 +108,6 @@ def unit_of_measurement(self): @property def icon(self) -> Optional[str]: """Return the icon to use in the frontend, if any.""" - icons = { DEVICE_CLASS_CO: "mdi:air-filter", DEVICE_CLASS_CO2: "mdi:periodic-table-co2", diff --git a/custom_components/tahoma/switch.py b/custom_components/tahoma/switch.py index e65675266..4ca19954f 100644 --- a/custom_components/tahoma/switch.py +++ b/custom_components/tahoma/switch.py @@ -13,15 +13,14 @@ async def async_setup_entry(hass, entry, async_add_entities): """Set up the TaHoma sensors from a config entry.""" - data = hass.data[DOMAIN][entry.entry_id] - - entities = [] controller = data.get("controller") - for device in data.get("devices"): - if TAHOMA_TYPES[device.uiclass] == "switch": - entities.append(TahomaSwitch(device, controller)) + entities = [ + TahomaSwitch(device, controller) + for device in data.get("devices") + if TAHOMA_TYPES[device.uiclass] == "switch" + ] async_add_entities(entities) @@ -37,7 +36,6 @@ def __init__(self, tahoma_device, controller): def update(self): """Update method.""" - if self.should_wait(): self.schedule_update_ha_state(True) return @@ -52,7 +50,6 @@ def update(self): @property def device_class(self): """Return the class of the device.""" - if self.tahoma_device.uiclass == "Siren": return DEVICE_CLASS_SIREN @@ -61,7 +58,6 @@ def device_class(self): @property def icon(self) -> Optional[str]: """Return the icon to use in the frontend, if any.""" - if self.device_class == DEVICE_CLASS_SIREN: if self.is_on: return "mdi:bell-ring" @@ -72,7 +68,6 @@ def icon(self) -> Optional[str]: def turn_on(self, **kwargs): """Send the on command.""" - if "on" in self.tahoma_device.command_definitions: return self.apply_action("on") @@ -84,17 +79,15 @@ def turn_on(self, **kwargs): def turn_off(self, **kwargs): """Send the off command.""" - if "off" in self.tahoma_device.command_definitions: return self.apply_action("off") def toggle(self, **kwargs): """Click the switch.""" - if "cycle" in self.tahoma_device.command_definitions: return self.apply_action("cycle") @property def is_on(self): """Get whether the switch is in on state.""" - return bool(self._state == STATE_ON) + return self._state == STATE_ON diff --git a/custom_components/tahoma/tahoma_device.py b/custom_components/tahoma/tahoma_device.py index fed2d2d70..45351f697 100644 --- a/custom_components/tahoma/tahoma_device.py +++ b/custom_components/tahoma/tahoma_device.py @@ -5,6 +5,7 @@ from .const import ( ATTR_RSSI_LEVEL, + CORE_BATTERY_STATE, CORE_RSSI_LEVEL_STATE, CORE_SENSOR_DEFECT_STATE, CORE_STATUS_STATE, @@ -36,16 +37,13 @@ def name(self): @property def available(self) -> bool: """Return True if entity is available.""" + states = self.tahoma_device.active_states - if CORE_STATUS_STATE in self.tahoma_device.active_states: - return bool( - self.tahoma_device.active_states.get(CORE_STATUS_STATE) == "available" - ) + if CORE_STATUS_STATE in states: + return states.get(CORE_STATUS_STATE) == "available" - if CORE_SENSOR_DEFECT_STATE in self.tahoma_device.active_states: - return ( - self.tahoma_device.active_states.get(CORE_SENSOR_DEFECT_STATE) != "dead" - ) + if CORE_SENSOR_DEFECT_STATE in states: + return states.get(CORE_SENSOR_DEFECT_STATE) != "dead" # A RTS power socket doesn't have a feedback channel, # so we must assume the socket is available. @@ -67,20 +65,19 @@ def assumed_state(self): @property def device_state_attributes(self): """Return the state attributes of the device.""" - attr = { "uiclass": self.tahoma_device.uiclass, "widget": self.tahoma_device.widget, "type": self.tahoma_device.type, } - if CORE_RSSI_LEVEL_STATE in self.tahoma_device.active_states: - attr[ATTR_RSSI_LEVEL] = self.tahoma_device.active_states[ - CORE_RSSI_LEVEL_STATE - ] + states = self.tahoma_device.active_states + + if CORE_RSSI_LEVEL_STATE in states: + attr[ATTR_RSSI_LEVEL] = states.get(CORE_RSSI_LEVEL_STATE) - if "core:BatteryState" in self.tahoma_device.active_states: - battery_state = self.tahoma_device.active_states["core:BatteryState"] + if CORE_BATTERY_STATE in states: + battery_state = states.get(CORE_BATTERY_STATE) if battery_state == "full": battery_state = 100 @@ -93,11 +90,11 @@ def device_state_attributes(self): attr[ATTR_BATTERY_LEVEL] = battery_state - if CORE_SENSOR_DEFECT_STATE in self.tahoma_device.active_states: - if self.tahoma_device.active_states.get(CORE_SENSOR_DEFECT_STATE) == "dead": + if CORE_SENSOR_DEFECT_STATE in states: + if states.get(CORE_SENSOR_DEFECT_STATE) == "dead": attr[ATTR_BATTERY_LEVEL] = 0 - for state_name, value in self.tahoma_device.active_states.items(): + for state_name, value in states.items(): if "State" in state_name: attr[state_name] = value @@ -126,7 +123,6 @@ async def async_apply_action(self, cmd_name, *args): def apply_action(self, cmd_name, *args): """Apply Action to Device.""" - action = Action(self.tahoma_device.url) action.add_command(cmd_name, *args) exec_id = self.controller.apply_actions("HomeAssistant", [action])