Skip to content

Commit

Permalink
Compatibility with Home Assistant 2022.3.0
Browse files Browse the repository at this point in the history
Import Coco Submodule in this module
  • Loading branch information
cebos committed Mar 3, 2022
1 parent 17dd189 commit d59f22d
Show file tree
Hide file tree
Showing 26 changed files with 1,432 additions and 40 deletions.
35 changes: 13 additions & 22 deletions custom_components/nhc2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from .const import DOMAIN, KEY_GATEWAY, CONF_SWITCHES_AS_LIGHTS
from .helpers import extract_versions

REQUIREMENTS = ['nhc2-coco==1.3.3']

_LOGGER = logging.getLogger(__name__)

DOMAIN = DOMAIN
Expand Down Expand Up @@ -59,10 +57,17 @@ async def async_setup(hass, config):

return True

FORWARD_PLATFORMS = (
"climate",
"switch",
"light",
"fan",
"cover"
)

async def async_setup_entry(hass, entry):
"""Create a NHC2 gateway."""
from nhc2_coco import CoCo
from .coco import CoCo
coco = CoCo(
address=entry.data[CONF_HOST],
username=entry.data[CONF_USERNAME],
Expand Down Expand Up @@ -95,25 +100,11 @@ def process_sysinfo(nhc2_sysinfo):
sw_version=nhc_version + ' - CoCo Image: ' + coco_image,
)

hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
entry, 'light')
)

hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
entry, 'switch')
)

hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
entry, 'cover')
)

hass.async_create_task(
hass.config_entries.async_forward_entry_setup(
entry, 'fan')
)
for platform in FORWARD_PLATFORMS:
_LOGGER.info("Forwarding platform: %s", platform)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)

return process_sysinfo

Expand Down
214 changes: 214 additions & 0 deletions custom_components/nhc2/climate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
"""Support for Niko Home Control II Thermostats."""
import logging

from homeassistant.components.climate import (
ATTR_TEMPERATURE,
ClimateEntity
)

from homeassistant.components.climate.const import (
ATTR_FAN_MODE,
ATTR_HVAC_MODE,
ATTR_PRESET_MODE,
ATTR_SWING_MODE,
HVAC_MODE_COOL,
HVAC_MODE_DRY,
HVAC_MODE_FAN_ONLY,
HVAC_MODE_HEAT,
HVAC_MODE_HEAT_COOL,
HVAC_MODE_OFF,
CURRENT_HVAC_HEAT,
CURRENT_HVAC_COOL,
CURRENT_HVAC_IDLE,
PRESET_SLEEP,
PRESET_COMFORT,
PRESET_ECO,
PRESET_NONE,
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_TEMPERATURE,
SUPPORT_TARGET_TEMPERATURE_RANGE
)

from .coco import CoCo
from .coco_climate import CoCoThermostat
from .coco_device_class import CoCoDeviceClass

from .const import DOMAIN, KEY_GATEWAY, BRAND, CLIMATE
from .helpers import nhc2_entity_processor

KEY_GATEWAY = KEY_GATEWAY
KEY_ENTITY = 'nhc2_thermostats'

_LOGGER = logging.getLogger(__name__)

HA_STATE_TO_NHC2 = {
HVAC_MODE_COOL: "Cooling",
HVAC_MODE_HEAT: "Heating",
HVAC_MODE_OFF: "None"
}

NHC2_TO_HA_STATE = {
"Cooling": HVAC_MODE_COOL,
"Heating": HVAC_MODE_HEAT,
"None": HVAC_MODE_OFF
}

NHC2_TO_HA_CURRENT_STATE = {
"Cooling": CURRENT_HVAC_COOL,
"Heating": CURRENT_HVAC_HEAT,
"None": CURRENT_HVAC_IDLE
}

HA_PRESET_TO_NHC2 = {
PRESET_NONE: "Off",
PRESET_ECO: "Eco",
PRESET_COMFORT: "Day",
PRESET_SLEEP: "Night"
}

HVAC_MODES_LIST = list(HA_STATE_TO_NHC2)
FEATURE_LIST = SUPPORT_PRESET_MODE | SUPPORT_TARGET_TEMPERATURE

async def async_setup_entry(hass, config_entry, async_add_entities):
"""Load NHC2 Thermostat basd on a Config Entry."""
hass.data.setdefault(KEY_ENTITY, {})[config_entry.entry_id] = []
gateway: CoCo = hass.data[KEY_GATEWAY][config_entry.entry_id]
_LOGGER.debug('Platform is starting')
gateway.get_devices(CoCoDeviceClass.THERMOSTATS,
nhc2_entity_processor(hass,
config_entry,
async_add_entities,
KEY_ENTITY,
lambda x: NHC2HassThermostat(x))
)

class NHC2HassThermostat(ClimateEntity):
"""Representation of a Niko Home Control II thermostat."""

def __init__(self, nhc2thermostat: CoCoThermostat):
"""Initialize a thermostat."""
self._nhc2thermostat = nhc2thermostat
self._current_temperature = nhc2thermostat.current_temperature
nhc2thermostat.on_change = self._on_change
_LOGGER.debug("Init new thermostat: %s", nhc2thermostat.name)
_LOGGER.info(self.name + " temperature: " + str(self._current_temperature))

def _on_change(self):
self.schedule_update_ha_state()

def update_properties(self):
"""Handle data changes for node values."""
pass

async def async_set_temperature(self, **kwargs):
"""Set new target temperatures."""
temp = float(kwargs.get(ATTR_TEMPERATURE))
self._nhc2thermostat.set_temperature(temp)
self.schedule_update_ha_state()

async def async_set_hvac_mode(self, hvac_mode):
"""Set new target hvac mode."""
pass

async def async_set_preset_mode(self, preset_mode):
"""Set new target preset mode."""
self._nhc2thermostat.set_preset_mode(preset_mode)
self.schedule_update_ha_state()

@property
def state(self):
"""Return the current temperature."""
return NHC2_TO_HA_STATE[self._nhc2thermostat.hvac_mode]

@property
def current_temperature(self):
"""Return the current temperature."""
return self._current_temperature

@property
def target_temperature(self):
"""Return the target temperature."""
return self._nhc2thermostat.target_temperature

@property
def hvac_action(self):
"""Return thermostat's current operation mode"""
return NHC2_TO_HA_CURRENT_STATE[self._nhc2thermostat.hvac_mode]

@property
def hvac_mode(self):
"""Return thermostat's operation mode"""
return NHC2_TO_HA_STATE[self._nhc2thermostat.hvac_mode]

@property
def hvac_modes(self):
"""Return the list of available hvac operation modes."""
return HVAC_MODES_LIST

@property
def preset_mode(self):
"""Return the current preset mode, e.g., home, away, temp."""
return self._nhc2thermostat.preset_mode

@property
def preset_modes(self):
"""Return the list off supported preset modes."""
return list(self._nhc2thermostat.preset_modes)

@property
def temperature_unit(self):
"""Return the unit of measurement."""
return self._nhc2thermostat.temperature_unit

@property
def target_temperature_low(self):
"""Return the minimum target temperature."""
return self._nhc2thermostat.target_temperature_low

@property
def target_temperature_high(self):
"""Return the maximum target temperature."""
return self._nhc2thermostat.target_temperature_high

@property
def target_temperature_step(self):
"""Return the target temperature step size."""
return self._nhc2thermostat.target_temperature_step

@property
def temp_min(self):
"""Return the minimum temperature."""
return self._nhc2thermostat.temp_min

@property
def temp_max(self):
"""Return the maximum temperature."""
return self._nhc2thermostat.temp_max

@property
def supported_features(self):
"""Return the list of supported features."""
return FEATURE_LIST

@property
def name(self):
"""Return the thermostats name."""
return self._nhc2thermostat.name

@property
def unique_id(self):
"""Return the thermostats UUID."""
return self._nhc2thermostat.uuid

@property
def device_info(self):
"""Return the device info."""
return {
"identifiers": {
(DOMAIN, self.unique_id)
},
"name": self.name,
"manufacturer": BRAND,
"model": CLIMATE,
"via_hub": (DOMAIN, self._nhc2thermostat.profile_creation_id),
}
Loading

0 comments on commit d59f22d

Please sign in to comment.