diff --git a/custom_components/tahoma/__init__.py b/custom_components/tahoma/__init__.py index 258ee098f..ec3d4f7a7 100644 --- a/custom_components/tahoma/__init__.py +++ b/custom_components/tahoma/__init__.py @@ -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 diff --git a/custom_components/tahoma/binary_sensor.py b/custom_components/tahoma/binary_sensor.py index f99a64059..3d87159c3 100644 --- a/custom_components/tahoma/binary_sensor.py +++ b/custom_components/tahoma/binary_sensor.py @@ -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, @@ -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) @@ -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 diff --git a/custom_components/tahoma/config_flow.py b/custom_components/tahoma/config_flow.py index abd029fed..4036f3e7e 100644 --- a/custom_components/tahoma/config_flow.py +++ b/custom_components/tahoma/config_flow.py @@ -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 diff --git a/custom_components/tahoma/const.py b/custom_components/tahoma/const.py index 1d037bd7d..da03a7651 100644 --- a/custom_components/tahoma/const.py +++ b/custom_components/tahoma/const.py @@ -38,8 +38,7 @@ "SunSensor": "sensor", } -# TaHoma internal device states CORE_ON_OFF_STATE = "core:OnOffState" -# Commands COMMAND_OFF = "off" +COMMAND_ON = "on" diff --git a/custom_components/tahoma/cover.py b/custom_components/tahoma/cover.py index 9c3689c31..8977e09af 100644 --- a/custom_components/tahoma/cover.py +++ b/custom_components/tahoma/cover.py @@ -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, @@ -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 @@ -218,64 +237,64 @@ 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): @@ -283,17 +302,17 @@ def supported_features(self): 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: @@ -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 diff --git a/custom_components/tahoma/light.py b/custom_components/tahoma/light.py index e68f273d9..9483c5dfe 100644 --- a/custom_components/tahoma/light.py +++ b/custom_components/tahoma/light.py @@ -11,15 +11,20 @@ SUPPORT_EFFECT, LightEntity, ) +from homeassistant.const import STATE_ON import homeassistant.util.color as color_util -from .const import COMMAND_OFF, CORE_ON_OFF_STATE, DOMAIN, TAHOMA_TYPES +from .const import COMMAND_OFF, COMMAND_ON, CORE_ON_OFF_STATE, DOMAIN, TAHOMA_TYPES from .tahoma_device import TahomaDevice _LOGGER = logging.getLogger(__name__) SCAN_INTERVAL = timedelta(seconds=30) +COMMAND_SET_INTENSITY = "setIntensity" +COMMAND_WINK = "wink" +COMMAND_SET_RGB = "setRGB" + CORE_BLUE_COLOR_INTENSITY_STATE = "core:BlueColorIntensityState" CORE_GREEN_COLOR_INTENSITY_STATE = "core:GreenColorIntensityState" CORE_LIGHT_INTENSITY_STATE = "core:LightIntensityState" @@ -76,13 +81,13 @@ def supported_features(self) -> int: """Flag supported features.""" supported_features = 0 - if "setIntensity" in self.tahoma_device.command_definitions: + if COMMAND_SET_INTENSITY in self.tahoma_device.command_definitions: supported_features |= SUPPORT_BRIGHTNESS - if "wink" in self.tahoma_device.command_definitions: + if COMMAND_WINK in self.tahoma_device.command_definitions: supported_features |= SUPPORT_EFFECT - if "setRGB" in self.tahoma_device.command_definitions: + if COMMAND_SET_RGB in self.tahoma_device.command_definitions: supported_features |= SUPPORT_COLOR return supported_features @@ -91,7 +96,7 @@ def turn_on(self, **kwargs) -> None: """Turn the light on.""" if ATTR_HS_COLOR in kwargs: self.apply_action( - "setRGB", + COMMAND_SET_RGB, *[ int(float(c)) for c in color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR]) @@ -100,14 +105,14 @@ def turn_on(self, **kwargs) -> None: if ATTR_BRIGHTNESS in kwargs: self._brightness = int(float(kwargs[ATTR_BRIGHTNESS]) / 255 * 100) - self.apply_action("setIntensity", self._brightness) + self.apply_action(COMMAND_SET_INTENSITY, self._brightness) elif ATTR_EFFECT in kwargs: self._effect = kwargs[ATTR_EFFECT] - self.apply_action("wink", 100) + self.apply_action(self._effect, 100) else: - self.apply_action("on") + self.apply_action(COMMAND_ON) self.async_write_ha_state() @@ -121,7 +126,7 @@ def turn_off(self, **kwargs) -> None: @property def effect_list(self) -> list: """Return the list of supported effects.""" - return ["wink"] + return [COMMAND_WINK] @property def effect(self) -> str: @@ -138,7 +143,7 @@ def update(self): states = self.tahoma_device.active_states - self._state = states.get(CORE_ON_OFF_STATE) == "on" + self._state = states.get(CORE_ON_OFF_STATE) == STATE_ON self._brightness = states.get(CORE_LIGHT_INTENSITY_STATE) self._hs_color = color_util.color_RGB_to_hs( diff --git a/custom_components/tahoma/lock.py b/custom_components/tahoma/lock.py index 14d60feb3..61c77621d 100644 --- a/custom_components/tahoma/lock.py +++ b/custom_components/tahoma/lock.py @@ -12,6 +12,13 @@ SCAN_INTERVAL = timedelta(seconds=120) +COMMAND_LOCK = "lock" +COMMAND_UNLOCK = "unlock" + +CORE_LOCKED_UNLOCKED_STATE = "core:LockedUnlockedState" + +TAHOMA_STATE_LOCKED = "locked" + async def async_setup_entry(hass, entry, async_add_entities): """Set up the TaHoma locks from a config entry.""" @@ -35,9 +42,6 @@ def __init__(self, tahoma_device, controller): """Initialize the device.""" super().__init__(tahoma_device, controller) self._lock_status = None - self._available = False - self._battery_level = None - self._name = None def update(self): """Update method.""" @@ -46,29 +50,21 @@ def update(self): return self.controller.get_states([self.tahoma_device]) - self._battery_level = self.tahoma_device.active_states["core:BatteryState"] - self._name = self.tahoma_device.active_states["core:NameState"] if ( - self.tahoma_device.active_states.get("core:LockedUnlockedState") - == STATE_LOCKED + self.tahoma_device.active_states.get(CORE_LOCKED_UNLOCKED_STATE) + == TAHOMA_STATE_LOCKED ): self._lock_status = STATE_LOCKED else: self._lock_status = STATE_UNLOCKED - self._available = ( - self.tahoma_device.active_states.get("core:AvailabilityState") - == "available" - ) def unlock(self, **kwargs): """Unlock method.""" - _LOGGER.debug("Unlocking %s", self._name) - self.apply_action("unlock") + self.apply_action(COMMAND_UNLOCK) def lock(self, **kwargs): """Lock method.""" - _LOGGER.debug("Locking %s", self._name) - self.apply_action("lock") + self.apply_action(COMMAND_LOCK) @property def name(self): @@ -84,12 +80,3 @@ def available(self): def is_locked(self): """Return True if the lock is locked.""" return self._lock_status == STATE_LOCKED - - @property - def device_state_attributes(self): - """Return the lock state attributes.""" - attr = {ATTR_BATTERY_LEVEL: self._battery_level} - super_attr = super().device_state_attributes - if super_attr is not None: - attr.update(super_attr) - return attr diff --git a/custom_components/tahoma/sensor.py b/custom_components/tahoma/sensor.py index d3c8cc4a7..c783483b2 100644 --- a/custom_components/tahoma/sensor.py +++ b/custom_components/tahoma/sensor.py @@ -33,6 +33,10 @@ CORE_MEASURED_VALUE_TYPE = "core:MeasuredValueType" CORE_RELATIVE_HUMIDITY_STATE = "core:RelativeHumidityState" CORE_SUN_ENERGY_STATE = "core:SunEnergyState" +CORE_TEMPERATURE_IN_CELCIUS = "core:TemperatureInCelcius" +CORE_TEMPERATURE_IN_CELSIUS = "core:TemperatureInCelsius" +CORE_TEMPERATURE_IN_FAHRENHEIT = "core:TemperatureInFahrenheit" +CORE_TEMPERATURE_IN_KELVIN = "core:TemperatureInKelvin" CORE_TEMPERATURE_STATE = "core:TemperatureState" CORE_WINDSPEED_STATE = "core:WindSpeedState" @@ -41,6 +45,13 @@ DEVICE_CLASS_SUN_ENERGY = "sun_energy" DEVICE_CLASS_WIND_SPEED = "wind_speed" +ICON_AIR_FILTER = "mdi:air-filter" +ICON_PERIODIC_TABLE_CO2 = "mdi:periodic-table-co2" +ICON_SOLAR_POWER = "mdi:solar-power" +ICON_WEATHER_WINDY = "mdi:weather-windy" + +UNIT_LX = "lx" + TAHOMA_SENSOR_DEVICE_CLASSES = { "TemperatureSensor": DEVICE_CLASS_TEMPERATURE, "HumiditySensor": DEVICE_CLASS_HUMIDITY, @@ -89,10 +100,10 @@ def unit_of_measurement(self): if CORE_TEMPERATURE_STATE in states: return { - "core:TemperatureInCelsius": TEMP_CELSIUS, - "core:TemperatureInCelcius": TEMP_CELSIUS, - "core:TemperatureInKelvin": TEMP_KELVIN, - "core:TemperatureInFahrenheit": TEMP_FAHRENHEIT, + CORE_TEMPERATURE_IN_CELSIUS: TEMP_CELSIUS, + CORE_TEMPERATURE_IN_CELCIUS: TEMP_CELSIUS, + CORE_TEMPERATURE_IN_KELVIN: TEMP_KELVIN, + CORE_TEMPERATURE_IN_FAHRENHEIT: TEMP_FAHRENHEIT, }.get( self.tahoma_device.attributes.get(CORE_MEASURED_VALUE_TYPE), TEMP_CELSIUS, @@ -102,7 +113,7 @@ def unit_of_measurement(self): return UNIT_PERCENTAGE if CORE_LUMINANCE_STATE in states: - return "lx" + return UNIT_LX if CORE_ELECTRIC_POWER_CONSUMPTION_STATE in states: return POWER_WATT @@ -122,10 +133,10 @@ def unit_of_measurement(self): 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", - DEVICE_CLASS_WIND_SPEED: "mdi:weather-windy", - DEVICE_CLASS_SUN_ENERGY: "mdi:solar-power", + DEVICE_CLASS_CO: ICON_AIR_FILTER, + DEVICE_CLASS_CO2: ICON_PERIODIC_TABLE_CO2, + DEVICE_CLASS_WIND_SPEED: ICON_WEATHER_WINDY, + DEVICE_CLASS_SUN_ENERGY: ICON_SOLAR_POWER, } return icons.get(self.device_class) diff --git a/custom_components/tahoma/switch.py b/custom_components/tahoma/switch.py index 59bc389d0..fb4a00e8d 100644 --- a/custom_components/tahoma/switch.py +++ b/custom_components/tahoma/switch.py @@ -5,11 +5,15 @@ from homeassistant.components.switch import DEVICE_CLASS_SWITCH, SwitchEntity from homeassistant.const import STATE_ON -from .const import COMMAND_OFF, CORE_ON_OFF_STATE, DOMAIN, TAHOMA_TYPES +from .const import COMMAND_OFF, COMMAND_ON, CORE_ON_OFF_STATE, DOMAIN, TAHOMA_TYPES from .tahoma_device import TahomaDevice _LOGGER = logging.getLogger(__name__) +COMMAND_CYCLE = "cycle" +COMMAND_MEMORIZED_VOLUME = "memorizedVolume" +COMMAND_RING_WITH_SINGLE_SIMPLE_SEQUENCE = "ringWithSingleSimpleSequence" + DEVICE_CLASS_SIREN = "siren" @@ -46,7 +50,7 @@ def update(self): if CORE_ON_OFF_STATE in self.tahoma_device.active_states: self.current_value = ( - self.tahoma_device.active_states.get(CORE_ON_OFF_STATE) == "on" + self.tahoma_device.active_states.get(CORE_ON_OFF_STATE) == STATE_ON ) @property @@ -70,13 +74,17 @@ 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") + if COMMAND_ON in self.tahoma_device.command_definitions: + return self.apply_action(COMMAND_ON) if "ringWithSingleSimpleSequence" in self.tahoma_device.command_definitions: # Values taken from iosiren.js (tahomalink.com). Parameter usage is currently unknown. return self.apply_action( - "ringWithSingleSimpleSequence", 120000, 75, 2, "memorizedVolume" + COMMAND_RING_WITH_SINGLE_SIMPLE_SEQUENCE, + 120000, + 75, + 2, + COMMAND_MEMORIZED_VOLUME, ) def turn_off(self, **kwargs): @@ -87,7 +95,7 @@ def turn_off(self, **kwargs): def toggle(self, **kwargs): """Click the switch.""" if "cycle" in self.tahoma_device.command_definitions: - return self.apply_action("cycle") + return self.apply_action(COMMAND_CYCLE) @property def is_on(self): diff --git a/custom_components/tahoma/tahoma_device.py b/custom_components/tahoma/tahoma_device.py index e13c25168..6bb958e78 100644 --- a/custom_components/tahoma/tahoma_device.py +++ b/custom_components/tahoma/tahoma_device.py @@ -8,11 +8,19 @@ ATTR_RSSI_LEVEL = "rssi_level" +CORE_AVAILABILITY_STATE = "core:AvailabilityState" CORE_BATTERY_STATE = "core:BatteryState" CORE_RSSI_LEVEL_STATE = "core:RSSILevelState" CORE_SENSOR_DEFECT_STATE = "core:SensorDefectState" CORE_STATUS_STATE = "core:StatusState" +STATE_AVAILABLE = "available" +STATE_BATTERY_FULL = "full" +STATE_BATTERY_NORMAL = "normal" +STATE_BATTERY_LOW = "low" +STATE_BATTERY_VERY_LOW = "verylow" +STATE_DEAD = "dead" + class TahomaDevice(Entity): """Representation of a TaHoma device entity.""" @@ -40,10 +48,13 @@ def available(self) -> bool: states = self.tahoma_device.active_states if CORE_STATUS_STATE in states: - return states.get(CORE_STATUS_STATE) == "available" + return states.get(CORE_STATUS_STATE) == STATE_AVAILABLE if CORE_SENSOR_DEFECT_STATE in states: - return states.get(CORE_SENSOR_DEFECT_STATE) != "dead" + return states.get(CORE_SENSOR_DEFECT_STATE) != STATE_DEAD + + if CORE_AVAILABILITY_STATE in states: + return states.get(CORE_AVAILABILITY_STATE) == STATE_AVAILABLE # A RTS power socket doesn't have a feedback channel, # so we must assume the socket is available. @@ -79,19 +90,19 @@ def device_state_attributes(self): if CORE_BATTERY_STATE in states: battery_state = states.get(CORE_BATTERY_STATE) - if battery_state == "full": + if battery_state == STATE_BATTERY_FULL: battery_state = 100 - elif battery_state == "normal": + elif battery_state == STATE_BATTERY_NORMAL: battery_state = 75 - elif battery_state == "low": + elif battery_state == STATE_BATTERY_LOW: battery_state = 25 - elif battery_state == "verylow": + elif battery_state == STATE_BATTERY_VERY_LOW: battery_state = 10 attr[ATTR_BATTERY_LEVEL] = battery_state if CORE_SENSOR_DEFECT_STATE in states: - if states.get(CORE_SENSOR_DEFECT_STATE) == "dead": + if states.get(CORE_SENSOR_DEFECT_STATE) == STATE_DEAD: attr[ATTR_BATTERY_LEVEL] = 0 for state_name, value in states.items():