Skip to content

Commit

Permalink
Add constants for all strings (#126)
Browse files Browse the repository at this point in the history
* [refactor] Add constants for all strings

* [refactor] Remove unused imports.

Related Github issues: #110
  • Loading branch information
vlebourl authored Jul 9, 2020
1 parent e74a036 commit ec60405
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 132 deletions.
8 changes: 1 addition & 7 deletions custom_components/tahoma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
import logging

from requests.exceptions import RequestException
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EXCLUDE, CONF_PASSWORD, CONF_USERNAME
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
discovery,
)

from .const import DOMAIN, TAHOMA_TYPES
from .tahoma_api import TahomaApi
Expand Down
36 changes: 24 additions & 12 deletions custom_components/tahoma/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@
DEVICE_CLASS_RAIN = "rain"
DEVICE_CLASS_WATER = "water"

ICON_WATER = "mdi:water"
ICON_WATER_OFF = "mdi:water-off"
ICON_WAVES = "mdi:waves"
ICON_WEATHER_RAINY = "mdi:weather-rainy"

IO_VIBRATION_STATE = "io:VibrationDetectedState"

STATE_OPEN = "open"
STATE_PERSON_INSIDE = "personInside"
STATE_DETECTED = "detected"
STATE_PRESSED = "pressed"

TAHOMA_BINARY_SENSOR_DEVICE_CLASSES = {
"AirFlowSensor": DEVICE_CLASS_GAS,
"CarButtonSensor": DEVICE_CLASS_BUTTON,
Expand Down Expand Up @@ -89,13 +99,13 @@ 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"
return ICON_WATER
else:
return "mdi:water-off"
return ICON_WATER_OFF

icons = {
DEVICE_CLASS_GAS: "mdi:waves",
DEVICE_CLASS_RAIN: "mdi:weather-rainy",
DEVICE_CLASS_GAS: ICON_WAVES,
DEVICE_CLASS_RAIN: ICON_WEATHER_RAINY,
}

return icons.get(self.device_class)
Expand All @@ -111,28 +121,30 @@ def update(self):
states = self.tahoma_device.active_states

if CORE_CONTACT_STATE in states:
self.current_value = states.get(CORE_CONTACT_STATE) == "open"
self.current_value = states.get(CORE_CONTACT_STATE) == STATE_OPEN

if CORE_OCCUPANCY_STATE in states:
self.current_value = states.get(CORE_OCCUPANCY_STATE) == "personInside"
self.current_value = states.get(CORE_OCCUPANCY_STATE) == STATE_PERSON_INSIDE

if CORE_SMOKE_STATE in states:
self.current_value = states.get(CORE_SMOKE_STATE) == "detected"
self.current_value = states.get(CORE_SMOKE_STATE) == STATE_DETECTED

if CORE_RAIN_STATE in states:
self.current_value = states.get(CORE_RAIN_STATE) == "detected"
self.current_value = states.get(CORE_RAIN_STATE) == STATE_DETECTED

if CORE_WATER_DETECTION_STATE in states:
self.current_value = states.get(CORE_WATER_DETECTION_STATE) == "detected"
self.current_value = (
states.get(CORE_WATER_DETECTION_STATE) == STATE_DETECTED
)

if CORE_GAS_DETECTION_STATE in states:
self.current_value = states.get(CORE_GAS_DETECTION_STATE) == "detected"
self.current_value = states.get(CORE_GAS_DETECTION_STATE) == STATE_DETECTED

if IO_VIBRATION_STATE in states:
self.current_value = states.get(IO_VIBRATION_STATE) == "detected"
self.current_value = states.get(IO_VIBRATION_STATE) == STATE_DETECTED

if CORE_BUTTON_STATE in states:
self.current_value = states.get(CORE_BUTTON_STATE) == "pressed"
self.current_value = states.get(CORE_BUTTON_STATE) == STATE_PRESSED

if self.current_value:
self._state = STATE_ON
Expand Down
7 changes: 1 addition & 6 deletions custom_components/tahoma/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
PRESET_COMFORT,
PRESET_ECO,
)
from homeassistant.const import (
CONF_ENTITY_ID,
CONF_PASSWORD,
CONF_USERNAME,
DEVICE_CLASS_TEMPERATURE,
)
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, DEVICE_CLASS_TEMPERATURE
from homeassistant.core import callback

from .const import DOMAIN
Expand Down
3 changes: 1 addition & 2 deletions custom_components/tahoma/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
"SunSensor": "sensor",
}

# TaHoma internal device states
CORE_ON_OFF_STATE = "core:OnOffState"

# Commands
COMMAND_OFF = "off"
COMMAND_ON = "on"
117 changes: 68 additions & 49 deletions custom_components/tahoma/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,40 @@
ATTR_LOCK_ORIG = "lock_originator"
ATTR_MEM_POS = "memorized_position"

COMMAND_CLOSE = "close"
COMMAND_CLOSE_SLATS = "closeSlats"
COMMAND_DOWN = "down"
COMMAND_MY = "my"
COMMAND_OPEN = "open"
COMMAND_OPEN_SLATS = "openSlats"
COMMAND_SET_CLOSURE = "setClosure"
COMMAND_SET_ORIENTATION = "setOrientation"
COMMAND_SET_PEDESTRIAN_POSITION = "setPedestrianPosition"
COMMAND_SET_POSITION = "setPosition"
COMMAND_STOP = "stop"
COMMAND_STOP_IDENTIFY = "stopIdentify"
COMMAND_UP = "up"

CORE_CLOSURE_STATE = "core:ClosureState"
CORE_DEPLOYMENT_STATE = "core:DeploymentState"
CORE_MEMORIZED_1_POSITION_STATE = "core:Memorized1PositionState"
CORE_OPEN_CLOSED_PARTIAL_STATE = "core:OpenClosedPartialState"
CORE_OPEN_CLOSED_PEDESTRIAN_STATE = "core:OpenClosedPedestrianState"
CORE_OPEN_CLOSED_STATE = "core:OpenClosedState"
CORE_OPEN_CLOSED_UNKNOWN_STATE = "core:OpenClosedUnknownState"
CORE_PEDESTRIAN_POSITION_STATE = "core:PedestrianPositionState"
CORE_PRIORITY_LOCK_TIMER_STATE = "core:PriorityLockTimerState"
CORE_SLATS_OPEN_CLOSED_STATE = "core:SlatsOpenClosedState"
CORE_SLATS_ORIENTATION_STATE = "core:SlatsOrientationState"
CORE_TARGET_CLOSURE_STATE = "core:TargetClosureState"

ICON_LOCK_ALERT = "mdi:lock-alert"
ICON_WEATHER_WINDY = "mdi:weather-windy"

IO_PRIORITY_LOCK_ORIGINATOR_STATE = "io:PriorityLockOriginatorState"

STATE_CLOSED = "closed"

TAHOMA_COVER_DEVICE_CLASSES = {
"Awning": DEVICE_CLASS_AWNING,
"ExteriorScreen": DEVICE_CLASS_BLIND,
Expand Down Expand Up @@ -162,20 +181,20 @@ def is_closed(self):
"""Return if the cover is closed."""

states = self.tahoma_device.active_states
if "core:OpenClosedState" in states:
return states.get("core:OpenClosedState") == "closed"
if CORE_OPEN_CLOSED_STATE in states:
return states.get(CORE_OPEN_CLOSED_STATE) == STATE_CLOSED

if "core:SlatsOpenClosedState" in states:
return states.get("core:SlatsOpenClosedState") == "closed"
if CORE_SLATS_OPEN_CLOSED_STATE in states:
return states.get(CORE_SLATS_OPEN_CLOSED_STATE) == STATE_CLOSED

if "core:OpenClosedPartialState" in states:
return states.get("core:OpenClosedPartialState") == "closed"
if CORE_OPEN_CLOSED_PARTIAL_STATE in states:
return states.get(CORE_OPEN_CLOSED_PARTIAL_STATE) == STATE_CLOSED

if "core:OpenClosedPedestrianState" in states:
return states.get("core:OpenClosedPedestrianState") == "closed"
if CORE_OPEN_CLOSED_PEDESTRIAN_STATE in states:
return states.get(CORE_OPEN_CLOSED_PEDESTRIAN_STATE) == STATE_CLOSED

if "core:OpenClosedUnknownState" in states:
return states.get("core:OpenClosedUnknownState") == "closed"
if CORE_OPEN_CLOSED_UNKNOWN_STATE in states:
return states.get(CORE_OPEN_CLOSED_UNKNOWN_STATE) == STATE_CLOSED

if self.current_cover_position is not None:
return self.current_cover_position == 0
Expand Down Expand Up @@ -218,82 +237,82 @@ def icon(self):
states = self.tahoma_device.active_states
if states.get(CORE_PRIORITY_LOCK_TIMER_STATE, 0) > 0:
if states.get(IO_PRIORITY_LOCK_ORIGINATOR_STATE) == "wind":
return "mdi:weather-windy"
return ICON_WEATHER_WINDY
else:
return "mdi:lock-alert"
return ICON_LOCK_ALERT
return None

def open_cover(self, **kwargs):
"""Open the cover."""

if "open" in self.tahoma_device.command_definitions:
return self.apply_action("open")
if COMMAND_OPEN in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_OPEN)

if "up" in self.tahoma_device.command_definitions:
return self.apply_action("up")
if COMMAND_UP in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_UP)

def open_cover_tilt(self, **kwargs):
"""Open the cover tilt."""

if "openSlats" in self.tahoma_device.command_definitions:
return self.apply_action("openSlats")
if COMMAND_OPEN_SLATS in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_OPEN_SLATS)

def close_cover(self, **kwargs):
"""Close the cover."""

if "close" in self.tahoma_device.command_definitions:
return self.apply_action("close")
if COMMAND_CLOSE in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_CLOSE)

if "down" in self.tahoma_device.command_definitions:
return self.apply_action("down")
if COMMAND_DOWN in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_DOWN)

def close_cover_tilt(self, **kwargs):
"""Close the cover tilt."""

if "closeSlats" in self.tahoma_device.command_definitions:
return self.apply_action("closeSlats")
if COMMAND_CLOSE_SLATS in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_CLOSE_SLATS)

def stop_cover(self, **kwargs):
"""Stop the cover."""

if "stop" in self.tahoma_device.command_definitions:
return self.apply_action("stop")
if COMMAND_STOP in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_STOP)

if "stopIdentify" in self.tahoma_device.command_definitions:
return self.apply_action("stopIdentify")
if COMMAND_STOP_IDENTIFY in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_STOP_IDENTIFY)

if "my" in self.tahoma_device.command_definitions:
return self.apply_action("my")
if COMMAND_MY in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_MY)

def stop_cover_tilt(self, **kwargs):
"""Stop the cover."""

if "stopIdentify" in self.tahoma_device.command_definitions:
return self.apply_action("stopIdentify")
if COMMAND_STOP_IDENTIFY in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_STOP_IDENTIFY)

if "stop" in self.tahoma_device.command_definitions:
return self.apply_action("stop")
if COMMAND_STOP in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_STOP)

if "my" in self.tahoma_device.command_definitions:
return self.apply_action("my")
if COMMAND_MY in self.tahoma_device.command_definitions:
return self.apply_action(COMMAND_MY)

@property
def supported_features(self):
"""Flag supported features."""

supported_features = 0

if "openSlats" in self.tahoma_device.command_definitions:
if COMMAND_OPEN_SLATS in self.tahoma_device.command_definitions:
supported_features |= SUPPORT_OPEN_TILT

if (
"stop" in self.tahoma_device.command_definitions
or "stopIdentify" in self.tahoma_device.command_definitions
or "my" in self.tahoma_device.command_definitions
COMMAND_STOP in self.tahoma_device.command_definitions
or COMMAND_STOP_IDENTIFY in self.tahoma_device.command_definitions
or COMMAND_MY in self.tahoma_device.command_definitions
):
supported_features |= SUPPORT_STOP_TILT

if "closeSlats" in self.tahoma_device.command_definitions:
if COMMAND_CLOSE_SLATS in self.tahoma_device.command_definitions:
supported_features |= SUPPORT_CLOSE_TILT

if COMMAND_SET_ORIENTATION in self.tahoma_device.command_definitions:
Expand All @@ -302,26 +321,26 @@ def supported_features(self):
if (
COMMAND_SET_POSITION in self.tahoma_device.command_definitions
or COMMAND_SET_CLOSURE in self.tahoma_device.command_definitions
or "setPedestrianPosition" in self.tahoma_device.command_definitions
or COMMAND_SET_PEDESTRIAN_POSITION in self.tahoma_device.command_definitions
):
supported_features |= SUPPORT_SET_POSITION

if (
"open" in self.tahoma_device.command_definitions
or "up" in self.tahoma_device.command_definitions
COMMAND_OPEN in self.tahoma_device.command_definitions
or COMMAND_UP in self.tahoma_device.command_definitions
):
supported_features |= SUPPORT_OPEN

if (
"stop" in self.tahoma_device.command_definitions
or "my" in self.tahoma_device.command_definitions
or "stopIdentify" in self.tahoma_device.command_definitions
COMMAND_STOP in self.tahoma_device.command_definitions
or COMMAND_MY in self.tahoma_device.command_definitions
or COMMAND_STOP_IDENTIFY in self.tahoma_device.command_definitions
):
supported_features |= SUPPORT_STOP

if (
"close" in self.tahoma_device.command_definitions
or "down" in self.tahoma_device.command_definitions
COMMAND_CLOSE in self.tahoma_device.command_definitions
or COMMAND_DOWN in self.tahoma_device.command_definitions
):
supported_features |= SUPPORT_CLOSE

Expand Down
Loading

0 comments on commit ec60405

Please sign in to comment.