Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code readability improvements #121

Merged
merged 11 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions custom_components/tahoma/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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):
Expand All @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions custom_components/tahoma/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,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"
Expand All @@ -134,6 +135,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_MEMORIZED_1_POSITION_STATE = "core:Memorized1PositionState"
CORE_NAME_STATE = "core:NameState"
Expand Down
1 change: 0 additions & 1 deletion custom_components/tahoma/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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 = [
Expand Down
42 changes: 19 additions & 23 deletions custom_components/tahoma/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand All @@ -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",
Expand All @@ -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")

Expand All @@ -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),
)
10 changes: 5 additions & 5 deletions custom_components/tahoma/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 1 addition & 5 deletions custom_components/tahoma/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 0 additions & 4 deletions custom_components/tahoma/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,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 = [
Expand Down Expand Up @@ -71,7 +69,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:
Expand Down Expand Up @@ -101,7 +98,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",
Expand Down
19 changes: 6 additions & 13 deletions custom_components/tahoma/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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"
Expand All @@ -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")

Expand All @@ -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
34 changes: 15 additions & 19 deletions custom_components/tahoma/tahoma_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from .const import (
ATTR_RSSI_LEVEL,
CORE_BATTERY_STATE,
CORE_RSSI_LEVEL_STATE,
CORE_SENSOR_DEFECT_STATE,
CORE_STATUS_STATE,
Expand Down Expand Up @@ -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.
Expand All @@ -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)
iMicknl marked this conversation as resolved.
Show resolved Hide resolved

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
Expand All @@ -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

Expand Down Expand Up @@ -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])
Expand Down