* Add MELCloud integration * Provides a climate and sensor platforms. Multiple platforms on one go is not the best option, but it does not make sense to remove them and commit them later either. * Email and access token are stored to the ConfigEntry. The token can be updated by adding the integration again with the same email address. The config flow is aborted and the update is performed on the background. * Run isort * Fix pylint errors * Run black * Increase coverage * Update pymelcloud dependency * Add HVAC_MODE_OFF emulation * Remove print * Update pymelcloud to enable device type filtering * Collapse except blocks and chain ClientNotReadys * Add preliminary documentation URL * Use list comp for creating model info Filters out empty model names form units. * f-string galore Dropped 'HVAC' from AtaDevice name template. * Delegate fan mode mapping to pymelcloud * Fix type annotation * Access AtaDevice through self._device * Prefer list comprehension * Update pymelcloud to leverage device type grouping The updated backend lib returns devices in a dict grouped by the device type. The devices do not necessarily need to be in a single list and this way isinstance is not required to extract devices by type. * Remove DOMAIN presence check This does not seem to make much sense after all. * Fix async_setup_entry Entry setup used half-baked naming from few experimentations back. The naming conventiens were unified to match the platforms. A redundant noneness check was also removed after evaluating the possible return values from the backend lib. * Simplify empty model name check * Improve config validation * Use config_validation strings. * Add CONF_EMAIL to config schema. The value is not strictly required when configuring through configuration.yaml, but having it there makes things more consistent. * Use dict[key] to access required properties. * Add DOMAIN in config check back to async_setup. This is required if an integration is configured throught config_flow. * Remove unused manifest properties * Remove redundant ClimateDevice property override * Add __init__.py to coverage exclusion * Use CONF_USERNAME instead of CONF_EMAIL * Use asyncio.gather instead of asyncio.wait * Misc fixes * any -> Any * Better names for dict iterations * Proper dict access with mandatory/known keys * Remove unused 'name' argument * Remove unnecessary platform info from unique_ids * Remove redundant methods from climate platform * Remove redundant default value from dict get * Update ConfigFlow sub-classing * Define sensors in a dict instead of a list * Use _abort_if_unique_id_configured to update token * Fix them tests * Remove current state guards * Fix that gather call * Implement sensor definitions without str manipulation * Use relative intra-package imports * Update homeassistant/components/melcloud/config_flow.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
171 lines
5.4 KiB
Python
171 lines
5.4 KiB
Python
"""Platform for climate integration."""
|
|
from datetime import timedelta
|
|
import logging
|
|
from typing import List, Optional
|
|
|
|
from pymelcloud import DEVICE_TYPE_ATA
|
|
|
|
from homeassistant.components.climate import ClimateDevice
|
|
from homeassistant.components.climate.const import (
|
|
DEFAULT_MAX_TEMP,
|
|
DEFAULT_MIN_TEMP,
|
|
HVAC_MODE_OFF,
|
|
SUPPORT_FAN_MODE,
|
|
SUPPORT_TARGET_TEMPERATURE,
|
|
)
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import TEMP_CELSIUS
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
|
from homeassistant.util.temperature import convert as convert_temperature
|
|
|
|
from . import MelCloudDevice
|
|
from .const import DOMAIN, HVAC_MODE_LOOKUP, HVAC_MODE_REVERSE_LOOKUP, TEMP_UNIT_LOOKUP
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=60)
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
|
):
|
|
"""Set up MelCloud device climate based on config_entry."""
|
|
mel_devices = hass.data[DOMAIN][entry.entry_id]
|
|
async_add_entities(
|
|
[AtaDeviceClimate(mel_device) for mel_device in mel_devices[DEVICE_TYPE_ATA]],
|
|
True,
|
|
)
|
|
|
|
|
|
class AtaDeviceClimate(ClimateDevice):
|
|
"""Air-to-Air climate device."""
|
|
|
|
def __init__(self, device: MelCloudDevice):
|
|
"""Initialize the climate."""
|
|
self._api = device
|
|
self._device = self._api.device
|
|
self._name = device.name
|
|
|
|
@property
|
|
def unique_id(self) -> Optional[str]:
|
|
"""Return a unique ID."""
|
|
return f"{self._device.serial}-{self._device.mac}"
|
|
|
|
@property
|
|
def name(self):
|
|
"""Return the display name of this light."""
|
|
return self._name
|
|
|
|
async def async_update(self):
|
|
"""Update state from MELCloud."""
|
|
await self._api.async_update()
|
|
|
|
@property
|
|
def device_info(self):
|
|
"""Return a device description for device registry."""
|
|
return self._api.device_info
|
|
|
|
@property
|
|
def temperature_unit(self) -> str:
|
|
"""Return the unit of measurement used by the platform."""
|
|
return TEMP_UNIT_LOOKUP.get(self._device.temp_unit, TEMP_CELSIUS)
|
|
|
|
@property
|
|
def hvac_mode(self) -> str:
|
|
"""Return hvac operation ie. heat, cool mode."""
|
|
mode = self._device.operation_mode
|
|
if not self._device.power or mode is None:
|
|
return HVAC_MODE_OFF
|
|
return HVAC_MODE_LOOKUP.get(mode)
|
|
|
|
async def async_set_hvac_mode(self, hvac_mode: str) -> None:
|
|
"""Set new target hvac mode."""
|
|
if hvac_mode == HVAC_MODE_OFF:
|
|
await self._device.set({"power": False})
|
|
return
|
|
|
|
operation_mode = HVAC_MODE_REVERSE_LOOKUP.get(hvac_mode)
|
|
if operation_mode is None:
|
|
raise ValueError(f"Invalid hvac_mode [{hvac_mode}]")
|
|
|
|
props = {"operation_mode": operation_mode}
|
|
if self.hvac_mode == HVAC_MODE_OFF:
|
|
props["power"] = True
|
|
await self._device.set(props)
|
|
|
|
@property
|
|
def hvac_modes(self) -> List[str]:
|
|
"""Return the list of available hvac operation modes."""
|
|
return [HVAC_MODE_OFF] + [
|
|
HVAC_MODE_LOOKUP.get(mode) for mode in self._device.operation_modes
|
|
]
|
|
|
|
@property
|
|
def current_temperature(self) -> Optional[float]:
|
|
"""Return the current temperature."""
|
|
return self._device.room_temperature
|
|
|
|
@property
|
|
def target_temperature(self) -> Optional[float]:
|
|
"""Return the temperature we try to reach."""
|
|
return self._device.target_temperature
|
|
|
|
async def async_set_temperature(self, **kwargs) -> None:
|
|
"""Set new target temperature."""
|
|
await self._device.set(
|
|
{"target_temperature": kwargs.get("temperature", self.target_temperature)}
|
|
)
|
|
|
|
@property
|
|
def target_temperature_step(self) -> Optional[float]:
|
|
"""Return the supported step of target temperature."""
|
|
return self._device.target_temperature_step
|
|
|
|
@property
|
|
def fan_mode(self) -> Optional[str]:
|
|
"""Return the fan setting."""
|
|
return self._device.fan_speed
|
|
|
|
async def async_set_fan_mode(self, fan_mode: str) -> None:
|
|
"""Set new target fan mode."""
|
|
await self._device.set({"fan_speed": fan_mode})
|
|
|
|
@property
|
|
def fan_modes(self) -> Optional[List[str]]:
|
|
"""Return the list of available fan modes."""
|
|
return self._device.fan_speeds
|
|
|
|
async def async_turn_on(self) -> None:
|
|
"""Turn the entity on."""
|
|
await self._device.set({"power": True})
|
|
|
|
async def async_turn_off(self) -> None:
|
|
"""Turn the entity off."""
|
|
await self._device.set({"power": False})
|
|
|
|
@property
|
|
def supported_features(self) -> int:
|
|
"""Return the list of supported features."""
|
|
return SUPPORT_FAN_MODE | SUPPORT_TARGET_TEMPERATURE
|
|
|
|
@property
|
|
def min_temp(self) -> float:
|
|
"""Return the minimum temperature."""
|
|
min_value = self._device.target_temperature_min
|
|
if min_value is not None:
|
|
return min_value
|
|
|
|
return convert_temperature(
|
|
DEFAULT_MIN_TEMP, TEMP_CELSIUS, self.temperature_unit
|
|
)
|
|
|
|
@property
|
|
def max_temp(self) -> float:
|
|
"""Return the maximum temperature."""
|
|
max_value = self._device.target_temperature_max
|
|
if max_value is not None:
|
|
return max_value
|
|
|
|
return convert_temperature(
|
|
DEFAULT_MAX_TEMP, TEMP_CELSIUS, self.temperature_unit
|
|
)
|