Skip to content
Open
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
46 changes: 45 additions & 1 deletion custom_components/gree_custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Standard library imports
from collections.abc import Mapping
import copy
import json
import logging
from typing import Any
Expand Down Expand Up @@ -31,7 +32,10 @@
from .aiogree.errors import GreeConnectionError
from .aiogree.transport_mqtt import GreeMqttTransport
from .aiogree.transport_udp import GreeUdpTransport
from .config_schema import CONFIG_SCHEMA as CONFIG_SCHEMA
from .config_schema import (
CONFIG_SCHEMA as CONFIG_SCHEMA,
options_without_temperature_step,
)

# Local imports
from .const import (
Expand All @@ -44,13 +48,16 @@
CONF_DISABLE_AVAILABLE_CHECK,
CONF_ENCRYPTION_KEY,
CONF_ENCRYPTION_VERSION,
CONF_HALF_DEGREES,
CONF_MAC_CONTROLLER_CLOUD,
CONF_MAC_CONTROLLER_LOCAL,
CONF_MAX_ONLINE_ATTEMPTS,
CONF_PREFER_CLOUD,
CONF_RESTORE_STATES,
CONF_UID,
CONFENTRY_ID_LOCAL_ONLY,
CURRENT_CONF_MINOR_VERSION,
CURRENT_CONF_VERSION,
DEFAULT_CONNECTION_MAX_ATTEMPTS,
DEFAULT_CONNECTION_TIMEOUT,
DEFAULT_DEVICE_PORT,
Expand Down Expand Up @@ -214,6 +221,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: GreeConfigEntry) -> bool
preferred_encryption_key=preferred_encryption_key,
user_id=uid,
)
half_degrees = options.get(CONF_HALF_DEGREES)
device.set_half_degrees(None if half_degrees is None else bool(half_degrees))

local_transport = (
None if prefer_cloud else local_transports.get(mac_controller_local)
Expand Down Expand Up @@ -307,6 +316,41 @@ async def async_setup_entry(hass: HomeAssistant, entry: GreeConfigEntry) -> bool
return True


async def async_migrate_entry(hass: HomeAssistant, entry: GreeConfigEntry) -> bool:
"""Bring an entry of an older minor version up to date.

Minor version 2 replaced the device option target_temp_step with
half_degrees. An entry of another major version is refused, as Home
Assistant did before this function existed.
"""
if entry.version != CURRENT_CONF_VERSION:
_LOGGER.error(
"Entry '%s' has version %s, this release only reads version %s",
entry.title,
entry.version,
CURRENT_CONF_VERSION,
)
return False

if entry.minor_version < 2:
data = copy.deepcopy(dict(entry.data))
for dev_config in data.get(CONF_DEVICES, {}).values():
options = dev_config.get(CONF_DEVICE_OPTIONS)
if options is not None:
dev_config[CONF_DEVICE_OPTIONS] = options_without_temperature_step(
options
)
hass.config_entries.async_update_entry(
entry, data=data, minor_version=CURRENT_CONF_MINOR_VERSION
)
_LOGGER.info(
"Entry '%s': the temperature step option is now Use half-degrees °C",
entry.title,
)

return True


async def _async_refresh_entry_token(
hass: HomeAssistant, entry: GreeConfigEntry, cloud_conf: Mapping[str, Any]
) -> dict[str, Any]:
Expand Down
7 changes: 5 additions & 2 deletions custom_components/gree_custom/aiogree/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class GreeProp(StrEnum):
# used to distinguish between Fahrenheit values, and the half degree in Celsius
TARGET_TEMPERATURE_BIT_1 = "TemRec"
TARGET_TEMPERATURE_BIT_2 = "StFahFlg"
# Other columns that some units use for the half degree in Celsius, see
# GreeDevice.set_target_temperature. Add0.5 is 0 or 1, Add0.1 is a tenths
# digit that the unit rounds to the nearest half degree.
TARGET_TEMPERATURE_HALF_C = "Add0.5"
TARGET_TEMPERATURE_TENTHS_C = "Add0.1"
# defines the unit of temperature for the target temperature
TARGET_TEMPERATURE_UNIT_1 = "TemUn"
TARGET_TEMPERATURE_UNIT_2 = "TemUnit" # zone controller and all its sub-units
Expand Down Expand Up @@ -144,8 +149,6 @@ class OtherProps(StrEnum):

UNKN_ACStupPos = "ACStupPos"
UNKN_ActiveTime = "ActiveTime"
UNKN_Add0_1 = "Add0.1"
UNKN_Add0_5 = "Add0.5"
UNKN_AirQ = "AirQ"
UNKN_AllErr = "AllErr"
UNKN_Antifreeze = "Antifreeze"
Expand Down
146 changes: 126 additions & 20 deletions custom_components/gree_custom/aiogree/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from collections.abc import Mapping
import logging
import math
from typing import Any

from aiomqtt import MqttError
Expand Down Expand Up @@ -141,6 +142,10 @@ def __init__(
self._target_temp_bit_prop: GreeProp | None = None
self._temp_unit_prop: GreeProp | None = None

# The user option "Use half-degrees °C". None when the user made no
# choice: see default_half_degrees.
self._half_degrees: bool | None = None

self._state = DeviceState(
device_id=self.unique_id, capabilities=self._capabilities
)
Expand Down Expand Up @@ -955,37 +960,120 @@ def set_horizontal_swing_mode(self, swing_mode: HorizontalSwingMode) -> None:
self._state.set(GreeProp.SWING_HORIZONTAL, swing_mode)

@property
def target_temperature_unit(self) -> TemperatureUnits:
"""Return the units of the target temperature."""
def supports_temperature_unit_change(self) -> bool:
"""Return True if the unit can be switched between Celsius and Fahrenheit.

The target temperature column is always Celsius. Only the bit column
tells the Fahrenheit degrees apart, so a unit without a bit is Celsius
only. The ducted unit of a zone controller is the exception: it has no
target temperature and no bit of its own, but its unit column sets the
unit of the whole system, and the zones do have the bit.
"""
if self._temp_unit_prop is None:
return False
return (
self._target_temp_bit_prop is not None
or self._device_type is DeviceType.ZONE_CONTROLLER
)

@property
def target_temperature_unit(self) -> TemperatureUnits:
"""Return the units of the target temperature.

Celsius when the unit cannot change units, see
supports_temperature_unit_change.
"""
if self._temp_unit_prop is None or not self.supports_temperature_unit_change:
return TemperatureUnits.C
return TemperatureUnits(
self._state.get(self._temp_unit_prop) or TemperatureUnits.C.value
)

def set_target_temperature_unit(self, units: TemperatureUnits) -> None:
"""Set the units of the target temperature."""
if self._temp_unit_prop is None:
_LOGGER.warning("[%s] The device has no temperature unit", self.unique_id)
if self._temp_unit_prop is None or not self.supports_temperature_unit_change:
_LOGGER.warning(
"[%s] The device cannot change its temperature unit", self.unique_id
)
return
self._state.set(self._temp_unit_prop, units)

@property
def has_target_temperature_bit(self) -> bool:
"""Return True if the device answers a bit for the target temperature.

Only then can it hold half degrees in Celsius, and every whole degree
in Fahrenheit. Without it, Fahrenheit goes in steps of about 2.
Only then can it hold every whole degree in Fahrenheit.
"""
return self._target_temp_bit_prop is not None

def _half_degree_props(self) -> list[GreeProp]:
"""Return the half degree columns the device answers.

These are the bit column (TemRec or StFahFlg), Add0.5 and Add0.1.
Answering a column is no proof that the unit uses it: some units
answer all three with 0. So all of them are written together, and
the unit takes the one it knows.
"""
props = [
prop
for prop in (
GreeProp.TARGET_TEMPERATURE_HALF_C,
GreeProp.TARGET_TEMPERATURE_TENTHS_C,
)
if self._state.supports(prop)
]
if self._target_temp_bit_prop is not None:
props.insert(0, self._target_temp_bit_prop)
return props

@property
def supports_half_degrees(self) -> bool:
"""Return True if the unit answers a column for half degrees Celsius."""
return self._target_temp_prop is not None and bool(self._half_degree_props())

@property
def half_degrees(self) -> bool:
"""Return True if half degrees Celsius are used.

This needs the user option and a unit that answers a half degree
column. Fahrenheit is always in whole degrees.
"""
choice = self._half_degrees
if choice is None:
choice = self.default_half_degrees
return choice and self.supports_half_degrees

@property
def default_half_degrees(self) -> bool:
"""Return the option "Use half-degrees °C" when the user made no choice.

On for a zone of a zone controller: its only half degree column is
StFahFlg, which the zones take, and before the option existed zones
always used half degrees. Off for every other unit, because some
answer a half degree column and still take only whole degrees.
"""
return self.device_type is DeviceType.ZONE

def set_half_degrees(self, value: bool | None) -> None:
"""Set the user option "Use half-degrees °C". None means no choice."""
self._half_degrees = value

@property
def target_temperature_step(self) -> float:
"""Return the step of the target temperature, in its unit."""
if self.target_temperature_unit is TemperatureUnits.C and self.half_degrees:
return 0.5
return 1.0

@property
def target_temperature(self) -> float | None:
"""Return the target temperature in target_temperature_unit.

The bit is the half degree in Celsius, and picks the value from the
table in Fahrenheit. Without a bit column it is 0. None until the
device is read, or when it has no target temperature.
In Celsius the value is a half degree when any answered half degree
column is set. A column the unit ignores reads 0, so it does no harm.
In Fahrenheit the bit picks the value from the table, and the other
columns are not used. None until the device is read, or when it has
no target temperature.
"""
if self._target_temp_prop is None:
return None
Expand All @@ -995,40 +1083,58 @@ def target_temperature(self) -> float | None:
return None

raw_c = raw + TARGET_TEMPERATURE_OFFSETS.get(self._target_temp_prop, 0)
bit = 0
if self._target_temp_bit_prop is not None:
bit = self._state.get(self._target_temp_bit_prop) or 0

if self.target_temperature_unit == TemperatureUnits.F:
bit = 0
if self._target_temp_bit_prop is not None:
bit = self._state.get(self._target_temp_bit_prop) or 0
return gree_get_target_temperature_f(raw_c, bit)
return gree_get_target_temperature_c(raw_c, bit)

half = any(self._state.get(prop) for prop in self._half_degree_props())
return gree_get_target_temperature_c(raw_c, 1 if half else 0)

def set_target_temperature(self, value: float) -> None:
"""Set the target temperature in target_temperature_unit.

Celsius goes to the nearest half degree, Fahrenheit to whole degrees.
The bit is sent along when the device answers it. A zone needs that,
because it keeps the old bit when only StTem is sent.
Celsius goes to the nearest half degree when half_degrees is on, and
to the nearest whole degree when it is off. Fahrenheit goes to whole
degrees. Every answered half degree column is sent along, also as 0
for a whole degree, so a half degree set with a remote is cleared:
the bit as 1, Add0.5 as 1 and Add0.1 as 5 for a half degree in
Celsius. In Fahrenheit the bit comes from the table and the Celsius
only columns are 0. A zone needs the bit, because it keeps the old
bit when only StTem is sent.
"""
if self._target_temp_prop is None:
_LOGGER.warning("[%s] The device has no target temperature", self.unique_id)
return

if self.target_temperature_unit == TemperatureUnits.F:
fahrenheit = self.target_temperature_unit == TemperatureUnits.F
if fahrenheit:
if not value.is_integer():
_LOGGER.warning(
"The Gree API does not support floating Fahrenheit values, the applied value will be: %.2f -> %d",
value,
round(value),
)
raw_c, bit = gree_get_target_temp_props_from_f(round(value))
else:
elif self.half_degrees:
raw_c, bit = gree_get_target_temp_props_from_c(value)
else:
# Round a half up, so 22.5 goes to 23, not to 22 as round() does
raw_c, bit = gree_get_target_temp_props_from_c(math.floor(value + 0.5))

offset = TARGET_TEMPERATURE_OFFSETS.get(self._target_temp_prop, 0)
props = {self._target_temp_prop: raw_c - offset}
if self._target_temp_bit_prop is not None:
props[self._target_temp_bit_prop] = bit
for prop in self._half_degree_props():
if prop is self._target_temp_bit_prop:
props[prop] = bit
elif fahrenheit:
props[prop] = 0
elif prop is GreeProp.TARGET_TEMPERATURE_TENTHS_C:
props[prop] = 5 * bit
else:
props[prop] = bit
self._state.update(props)

if self.feature_smart_heat or self.feature_energy_saving:
Expand Down
10 changes: 2 additions & 8 deletions custom_components/gree_custom/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@
CONF_HVAC_MODES,
CONF_SWING_HORIZONTAL_MODES,
CONF_SWING_MODES,
CONF_TEMPERATURE_STEP,
DEFAULT_FAN_MODES,
DEFAULT_HVAC_MODES,
DEFAULT_SWING_HORIZONTAL_MODES,
DEFAULT_SWING_MODES,
DEFAULT_TARGET_TEMP_STEP,
DOMAIN,
GATTR_FEAT_QUIET_MODE,
GATTR_FEAT_TURBO,
Expand Down Expand Up @@ -188,9 +186,6 @@ async def async_setup_entry(
fan_modes,
swing_modes,
swing_horizontal_modes,
temperature_step=options.get(
CONF_TEMPERATURE_STEP, DEFAULT_TARGET_TEMP_STEP
),
restore_state=coordinator.restore_states,
check_availability=coordinator.check_availability,
external_temperature_sensor_id=options.get(
Expand All @@ -216,7 +211,6 @@ def __init__(
fan_modes: list[str],
swing_modes: list[str],
swing_horizontal_modes: list[str],
temperature_step: int,
restore_state: bool = True,
check_availability: bool = True,
external_temperature_sensor_id: str | None = None,
Expand All @@ -233,8 +227,6 @@ def __init__(
# Last external sensor value and its unit
self._external_temperature: tuple[float, str] | None = None

self._attr_target_temperature_step = temperature_step

self._attr_hvac_modes = hvac_modes

if hvac_modes and HVACMode.OFF in hvac_modes:
Expand Down Expand Up @@ -587,6 +579,8 @@ def _update_attributes(self) -> None:

self._attr_native_temperature_unit = self.get_temp_units()
self._attr_native_target_temperature = self.get_current_target_temp()
# Follows the unit and the "Use half-degrees °C" option
self._attr_target_temperature_step = self.device.target_temperature_step

if (
self._external_temperature_sensor is None
Expand Down
Loading
Loading