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

Move constants outside of const.py #125

Merged
merged 4 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
54 changes: 36 additions & 18 deletions custom_components/tahoma/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,49 @@
import logging
from typing import Optional

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.const import ATTR_BATTERY_LEVEL, STATE_OFF, STATE_ON

from .const import (
CORE_BUTTON_STATE,
CORE_CONTACT_STATE,
CORE_GAS_DETECTION_STATE,
CORE_OCCUPANCY_STATE,
CORE_RAIN_STATE,
CORE_SMOKE_STATE,
CORE_WATER_DETECTION_STATE,
DEVICE_CLASS_GAS,
DEVICE_CLASS_RAIN,
DEVICE_CLASS_WATER,
DOMAIN,
IO_VIBRATION_STATE,
TAHOMA_BINARY_SENSOR_DEVICE_CLASSES,
TAHOMA_TYPES,
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_MOTION,
DEVICE_CLASS_OCCUPANCY,
DEVICE_CLASS_OPENING,
DEVICE_CLASS_SMOKE,
BinarySensorEntity,
)
from homeassistant.const import STATE_OFF, STATE_ON

from .const import DOMAIN, TAHOMA_TYPES
from .tahoma_device import TahomaDevice

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(seconds=120)

CORE_BUTTON_STATE = "core:ButtonState"
CORE_CONTACT_STATE = "core:ContactState"
CORE_GAS_DETECTION_STATE = "core:GasDetectionState"
CORE_OCCUPANCY_STATE = "core:OccupancyState"
CORE_RAIN_STATE = "core:RainState"
CORE_SMOKE_STATE = "core:SmokeState"
CORE_WATER_DETECTION_STATE = "core:WaterDetectionState"

DEVICE_CLASS_BUTTON = "button"
DEVICE_CLASS_GAS = "gas"
DEVICE_CLASS_RAIN = "rain"
DEVICE_CLASS_WATER = "water"

IO_VIBRATION_STATE = "io:VibrationDetectedState"

TAHOMA_BINARY_SENSOR_DEVICE_CLASSES = {
"AirFlowSensor": DEVICE_CLASS_GAS,
"CarButtonSensor": DEVICE_CLASS_BUTTON,
"SmokeSensor": DEVICE_CLASS_SMOKE,
"OccupancySensor": DEVICE_CLASS_OCCUPANCY,
"MotionSensor": DEVICE_CLASS_MOTION,
"ContactSensor": DEVICE_CLASS_OPENING,
"WindowHandle": DEVICE_CLASS_OPENING,
"RainSensor": DEVICE_CLASS_RAIN,
"WaterDetectionSensor": DEVICE_CLASS_WATER,
}


async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the TaHoma sensors from a config entry."""
Expand Down
69 changes: 33 additions & 36 deletions custom_components/tahoma/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import (
ATTR_PRESET_MODE,
CURRENT_HVAC_HEAT,
CURRENT_HVAC_IDLE,
CURRENT_HVAC_OFF,
HVAC_MODE_AUTO,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
PRESET_AWAY,
PRESET_BOOST,
PRESET_COMFORT,
PRESET_ECO,
PRESET_HOME,
Expand All @@ -34,38 +32,32 @@
from homeassistant.core import State, callback
from homeassistant.helpers.event import async_track_state_change

from .const import (
COMMAND_EXIT_DEROGATION,
COMMAND_OFF,
COMMAND_REFRESH_STATE,
COMMAND_SET_DEROGATION,
COMMAND_SET_HEATING_LEVEL,
COMMAND_SET_MODE_TEMPERATURE,
CORE_DEROGATED_TARGET_TEMPERATURE_STATE,
CORE_ON_OFF_STATE,
CORE_TARGET_TEMPERATURE_STATE,
DOMAIN,
IO_TARGET_HEATING_LEVEL_STATE,
ST_DEROGATION_HEATING_MODE_STATE,
ST_DEROGATION_TYPE_STATE,
ST_HEATING_MODE_STATE,
TAHOMA_TYPES,
)
from .const import COMMAND_OFF, CORE_ON_OFF_STATE, DOMAIN, TAHOMA_TYPES
from .tahoma_device import TahomaDevice

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(seconds=60)

W_ST = "SomfyThermostat"
W_AEH = "AtlanticElectricalHeater"
COMMAND_EXIT_DEROGATION = "exitDerogation"
COMMAND_REFRESH_STATE = "refreshState"
COMMAND_SET_DEROGATION = "setDerogation"
COMMAND_SET_HEATING_LEVEL = "setHeatingLevel"
COMMAND_SET_MODE_TEMPERATURE = "setModeTemperature"

CORE_DEROGATED_TARGET_TEMPERATURE_STATE = "core:DerogatedTargetTemperatureState"
CORE_TARGET_TEMPERATURE_STATE = "core:TargetTemperatureState"

SUPPORTED_CLIMATE_DEVICES = [W_ST, W_AEH]
IO_TARGET_HEATING_LEVEL_STATE = "io:TargetHeatingLevelState"

PRESET_FREEZE = "freeze"
PRESET_FROST_PROTECTION = "frostprotection"
PRESET_OFF = "off"

ST_DEROGATION_TYPE_STATE = "somfythermostat:DerogationTypeState"
ST_HEATING_MODE_STATE = "somfythermostat:HeatingModeState"
ST_DEROGATION_HEATING_MODE_STATE = "somfythermostat:DerogationHeatingModeState"

STATE_DEROGATION_FURTHER_NOTICE = "further_notice"
STATE_DEROGATION_NEXT_MODE = "next_mode"
STATE_DEROGATION_DATE = "date"
Expand All @@ -75,6 +67,11 @@
STATE_PRESET_MANUAL = "manualMode"
STATE_PRESET_SLEEPING_MODE = "sleepingMode"

ST = "SomfyThermostat"
AEH = "AtlanticElectricalHeater"

SUPPORTED_CLIMATE_DEVICES = [ST, AEH]

MAP_HVAC_MODE = {
STATE_DEROGATION_DATE: HVAC_MODE_AUTO,
STATE_DEROGATION_NEXT_MODE: HVAC_MODE_HEAT,
Expand Down Expand Up @@ -134,7 +131,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
if DEVICE_CLASS_TEMPERATURE in options:
if device.url in options[DEVICE_CLASS_TEMPERATURE]:
sensor_id = options[DEVICE_CLASS_TEMPERATURE][device.url]
if device.widget == W_ST:
if device.widget == ST:
entities.append(
TahomaClimate(device, controller, sensor_id, preset_temp)
)
Expand Down Expand Up @@ -167,7 +164,7 @@ async def update_listener(hass, entry):
entity.set_temperature_sensor(
options[DEVICE_CLASS_TEMPERATURE][entity.unique_id]
)
if entity.widget != W_ST:
if entity.widget != ST:
preset_temp = {
PRESET_FREEZE: None,
PRESET_AWAY: None,
Expand Down Expand Up @@ -205,7 +202,7 @@ def __init__(self, tahoma_device, controller, sensor_id=None, preset_temp=None):
self._target_temp = None
self._saved_target_temp = self._target_temp
self._preset_temperatures = preset_temp
if self._widget == W_ST:
if self._widget == ST:
self._hvac_modes = [HVAC_MODE_AUTO, HVAC_MODE_HEAT]
self._hvac_mode = MAP_HVAC_MODE[
self.tahoma_device.active_states[ST_DEROGATION_TYPE_STATE]
Expand All @@ -229,7 +226,7 @@ def __init__(self, tahoma_device, controller, sensor_id=None, preset_temp=None):
CORE_DEROGATED_TARGET_TEMPERATURE_STATE
]
)
elif self._widget == W_AEH:
elif self._widget == AEH:
self._hvac_modes = [HVAC_MODE_HEAT, HVAC_MODE_OFF]
self._hvac_mode = MAP_HVAC_MODE[
self.tahoma_device.active_states[CORE_ON_OFF_STATE]
Expand Down Expand Up @@ -274,7 +271,7 @@ async def _async_temp_sensor_changed(
return

self.update_temp(new_state)
if self._widget == W_AEH:
if self._widget == AEH:
await self.hass.async_add_executor_job(self._control_heating)
self.schedule_update_ha_state()

Expand Down Expand Up @@ -305,7 +302,7 @@ def update(self):

self.controller.get_states([self.tahoma_device])
self.update_temp(None)
if self._widget == W_ST:
if self._widget == ST:
self._hvac_mode = MAP_HVAC_MODE[
self.tahoma_device.active_states[ST_DEROGATION_TYPE_STATE]
]
Expand Down Expand Up @@ -362,7 +359,7 @@ def set_hvac_mode(self, hvac_mode: str) -> None:
"""Set new target hvac mode."""
if hvac_mode == self._hvac_mode:
return
if self._widget == W_ST:
if self._widget == ST:
if hvac_mode == HVAC_MODE_AUTO:
self._stored_target_temp = self._target_temp
self.apply_action(COMMAND_EXIT_DEROGATION)
Expand All @@ -375,7 +372,7 @@ def set_hvac_mode(self, hvac_mode: str) -> None:
STATE_DEROGATION_FURTHER_NOTICE,
)
self.apply_action(COMMAND_REFRESH_STATE)
if self._widget == W_AEH:
if self._widget == AEH:
self._hvac_mode = hvac_mode
self._control_heating()
return
Expand Down Expand Up @@ -423,7 +420,7 @@ def set_temperature(self, **kwargs) -> None:
temperature = kwargs.get(ATTR_TEMPERATURE)
if temperature is None:
return
if self._widget == W_ST:
if self._widget == ST:
if temperature < 15:
self.apply_action(
COMMAND_SET_DEROGATION,
Expand All @@ -440,7 +437,7 @@ def set_temperature(self, **kwargs) -> None:
COMMAND_SET_MODE_TEMPERATURE, STATE_PRESET_MANUAL, temperature
)
self.apply_action(COMMAND_REFRESH_STATE)
if self._widget == W_AEH:
if self._widget == AEH:
self._target_temp = temperature
self._control_heating()

Expand All @@ -459,12 +456,12 @@ def _control_heating(self) -> None:

def turn_off(self):
"""Turn the entity off."""
if self._widget == W_AEH:
if self._widget == AEH:
self.apply_action(COMMAND_OFF)

def turn_on(self):
"""Turn the entity on."""
if self._widget == W_AEH:
if self._widget == AEH:
self.apply_action(
COMMAND_SET_HEATING_LEVEL, AEH_MAP_PRESET_REVERSE[PRESET_COMFORT]
)
Expand Down Expand Up @@ -495,7 +492,7 @@ def set_preset_mode(self, preset_mode: str) -> None:
if self._preset_mode == preset_mode:
return
self._preset_mode = preset_mode
if self._widget == W_ST:
if self._widget == ST:
if preset_mode in [PRESET_FREEZE, PRESET_SLEEP, PRESET_AWAY, PRESET_HOME]:
self._stored_target_temp = self._target_temp
self.apply_action(
Expand All @@ -514,7 +511,7 @@ def set_preset_mode(self, preset_mode: str) -> None:
COMMAND_SET_MODE_TEMPERATURE, STATE_PRESET_MANUAL, self._target_temp
)
self.apply_action(COMMAND_REFRESH_STATE)
elif self._widget == W_AEH:
elif self._widget == AEH:
if preset_mode == PRESET_NONE:
self._preset_mode = PRESET_NONE
self._target_temp = self._saved_target_temp
Expand Down
Loading