Skip to content

Commit

Permalink
Merge pull request #126 from mmaarrkk02/main
Browse files Browse the repository at this point in the history
fix: swing mode and some bug
  • Loading branch information
jeatheak authored Dec 18, 2024
2 parents b9cdd16 + 20d8a61 commit 4d27717
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 149 deletions.
41 changes: 29 additions & 12 deletions custom_components/mitsubishi_wf_rac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@
CONF_AIRCO_ID,
CONF_AVAILABILITY_CHECK,
CONF_AVAILABILITY_RETRY_LIMIT,
CONF_OPERATOR_ID
CONF_OPERATOR_ID, CONF_CREATE_SWING_MODE_SELECT, DOMAIN
)
from .wfrac.device import Device

_LOGGER = logging.getLogger(__name__)

PLATFORMS = [Platform.CLIMATE, Platform.SELECT, Platform.SENSOR]


@dataclass
class MitsubishiWfRacData:
"""Class for storing runtime data."""
device: Device


type MitsubishiWfRacConfigEntry = ConfigEntry[MitsubishiWfRacData]


async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Migrate old config entry."""

Expand Down Expand Up @@ -62,18 +65,14 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def async_setup_entry(hass: HomeAssistant, entry: MitsubishiWfRacConfigEntry):
"""Establish connection with mitsubishi-wf-rac."""
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = {}
hass.data[DOMAIN][entry.entry_id]["devices"] = coordinators = []

device: str = entry.options[CONF_HOST]
name: str = entry.data[CONF_NAME]
device_id: str = entry.data[CONF_DEVICE_ID]
operator_id: str = entry.data[CONF_OPERATOR_ID]
port: int = entry.data[CONF_PORT]
airco_id: str = entry.data[CONF_AIRCO_ID]
availability_retry: bool = entry.options.get("availability_retry", False)
availability_retry_limit: int = entry.options.get("availability_retry_limit", 3)

_device = Device(hass, name, device, port, device_id, operator_id, airco_id, availability_retry, availability_retry_limit)
_device = await create_device_from_entry(entry, hass)

coordinators.append(_device)
try:
await _device.update() # initial update to get fresh values
entry.runtime_data = MitsubishiWfRacData(_device)
Expand All @@ -84,6 +83,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: MitsubishiWfRacConfigEnt

return True


async def create_device_from_entry(entry, hass):
device: str = entry.options[CONF_HOST]
name: str = entry.data[CONF_NAME]
device_id: str = entry.data[CONF_DEVICE_ID]
operator_id: str = entry.data[CONF_OPERATOR_ID]
port: int = entry.data[CONF_PORT]
airco_id: str = entry.data[CONF_AIRCO_ID]
availability_retry: bool = entry.options.get("availability_retry", False)
availability_retry_limit: int = entry.options.get(CONF_AVAILABILITY_RETRY_LIMIT, 3)
create_swing_mode_select: bool = entry.data.get(CONF_CREATE_SWING_MODE_SELECT, True)
_device = Device(hass, name, device, port, device_id, operator_id, airco_id, availability_retry,
availability_retry_limit, create_swing_mode_select)
return _device


async def async_unload_entry(hass: HomeAssistant, entry: MitsubishiWfRacConfigEntry) -> bool:
"""Handle unload of entry."""

Expand All @@ -97,6 +112,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: MitsubishiWfRacConfigEn

return unload_ok


async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update options."""
reloaded = await hass.config_entries.async_reload(entry.entry_id)
Expand All @@ -106,10 +122,11 @@ async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
else:
_LOGGER.warning("Failed to update options to [%s]", entry.options)


async def async_remove_entry(hass, entry: MitsubishiWfRacConfigEntry) -> None:
"""Handle removal of an entry."""

temp_device: Device = entry.runtime_data.device
temp_device = await create_device_from_entry(entry, hass)
try:
await temp_device.delete_account()
_LOGGER.info(
Expand All @@ -120,6 +137,6 @@ async def async_remove_entry(hass, entry: MitsubishiWfRacConfigEntry) -> None:
except Exception as ex: # pylint: disable=broad-except
_LOGGER.warning(
"Something whent wrong deleting account from airco [%s] %s",
temp_device.name,
temp_device.device_name,
ex,
)
98 changes: 44 additions & 54 deletions custom_components/mitsubishi_wf_rac/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
SUPPORTED_HVAC_MODES,
SWING_3D_AUTO,
SWING_MODE_TRANSLATION,
HORIZONTAL_SWING_MODE_TRANSLATION,
MIN_TIME_BETWEEN_UPDATES
SWING_HORIZONTAL_MODE_TRANSLATION,
MIN_TIME_BETWEEN_UPDATES,
SUPPORT_SWING_HORIZONTAL_MODES
)

_LOGGER = logging.getLogger(__name__)
Expand All @@ -44,7 +45,7 @@
async def async_setup_entry(hass, entry: MitsubishiWfRacConfigEntry, async_add_entities):
"""Setup climate entities"""
device: Device = entry.runtime_data.device
_LOGGER.info("Setup climate for: %s, %s", device.name, device.airco_id)
_LOGGER.info("Setup climate for: %s, %s", device.device_name, device.airco_id)
async_add_entities([AircoClimate(device, hass)])

platform = entity_platform.async_get_current_platform()
Expand All @@ -54,7 +55,7 @@ async def async_setup_entry(hass, entry: MitsubishiWfRacConfigEntry, async_add_e
{
vol.Required("swing_mode"): cv.string,
},
"async_set_horizontal_swing_mode",
"async_set_swing_horizontal_mode",
)

platform.async_register_entity_service(
Expand All @@ -79,26 +80,20 @@ class AircoClimate(ClimateEntity):
_attr_swing_modes: list[str] | None = SUPPORT_SWING_MODES
_attr_min_temp: float = 16
_attr_max_temp: float = 30
_attr_horizontal_swing_mode: str | None = SWING_HORIZONTAL_AUTO
_attr_swing_horizontal_mode: str | None = SWING_HORIZONTAL_AUTO
_attr_swing_horizontal_modes: list[str] | None = SUPPORT_SWING_HORIZONTAL_MODES
_enable_turn_on_off_backwards_compatibility = False # Remove after HA 2025.1

def __init__(self, device: Device, hass: HomeAssistant) -> None:
self._device = device
self._hass = hass

self._attr_name = device.name
self._attr_name = device.device_name
self._attr_device_info = device.device_info
self._attr_unique_id = f"{DOMAIN}-{self._device.airco_id}-climate"
self._consolidated_params = {}
self._update_state()

@property
def extra_state_attributes(self):
return {
"hvac_mode_state": self._attr_hvac_mode,
"horizontal_swing_mode": self._attr_horizontal_swing_mode
}

async def async_set_temperature(self, **kwargs) -> None:
"""Set new target temperature."""
set_temp = kwargs.get(ATTR_TEMPERATURE)
Expand Down Expand Up @@ -148,46 +143,37 @@ async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:

async def async_set_swing_mode(self, swing_mode: str) -> None:
"""Set new target swing operation."""
_airco = self._device.airco
_swing_auto = swing_mode == SWING_3D_AUTO
_swing_lr = (
HORIZONTAL_SWING_MODE_TRANSLATION[SWING_HORIZONTAL_AUTO]
if self._device.airco.Entrust
else self._device.airco.WindDirectionLR
)
_swing_ud = _airco.WindDirectionUD

if swing_mode != SWING_3D_AUTO:
_swing_ud = SWING_MODE_TRANSLATION[swing_mode]

await self._set_airco(
{
AirconCommands.WindDirectionUD: _swing_ud,
AirconCommands.WindDirectionLR: _swing_lr,
AirconCommands.Entrust: _swing_auto,
}
)
if _swing_auto:
await self._set_airco(
{
AirconCommands.Entrust: _swing_auto,
}
)
else:
await self._set_airco(
{
AirconCommands.WindDirectionUD: SWING_MODE_TRANSLATION[swing_mode],
AirconCommands.Entrust: False,
}
)

async def async_set_horizontal_swing_mode(self, swing_mode: str) -> None:
async def async_set_swing_horizontal_mode(self, swing_mode: str) -> None:
"""Set new target horizontal swing operation."""
_airco = self._device.airco
_swing_lr = HORIZONTAL_SWING_MODE_TRANSLATION[swing_mode]
_swing_ud = (
HORIZONTAL_SWING_MODE_TRANSLATION[SWING_VERTICAL_AUTO]
if self._device.airco.Entrust
else self._device.airco.WindDirectionUD
)

_LOGGER.debug("airco: %s", _airco)

await self._set_airco(
{
AirconCommands.WindDirectionUD: _swing_ud,
AirconCommands.WindDirectionLR: _swing_lr,
# always set to false otherwise service won't have effect
AirconCommands.Entrust: False,
}
)
_swing_auto = swing_mode == SWING_3D_AUTO
if _swing_auto:
await self._set_airco(
{
AirconCommands.Entrust: _swing_auto,
}
)
else:
await self._set_airco(
{
AirconCommands.WindDirectionLR: SWING_HORIZONTAL_MODE_TRANSLATION[swing_mode],
AirconCommands.Entrust: False,
}
)

async def async_turn_off(self) -> None:
"""Turn the entity off."""
Expand Down Expand Up @@ -220,9 +206,13 @@ def _update_state(self) -> None:
if airco.Entrust
else list(SWING_MODE_TRANSLATION.keys())[airco.WindDirectionUD]
)
self._attr_horizontal_swing_mode = list(
HORIZONTAL_SWING_MODE_TRANSLATION.keys()
)[airco.WindDirectionLR]
self._attr_swing_horizontal_mode = (
SWING_3D_AUTO
if airco.Entrust
else list(
SWING_HORIZONTAL_MODE_TRANSLATION.keys()
)[airco.WindDirectionLR]
)
self._attr_available = self._device.available
self._attr_hvac_mode = list(HVAC_TRANSLATION.keys())[airco.OperationMode]

Expand All @@ -249,7 +239,7 @@ async def async_update(self):
try:
await self._device.update()
self._update_state()
except Exception: # pylint: disable=broad-except
except Exception: # pylint: disable=broad-except
_LOGGER.warning("Could not update the airco values")
self._attr_available = False
self._device.set_available(False)
Expand Down
28 changes: 15 additions & 13 deletions custom_components/mitsubishi_wf_rac/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult

from .const import CONF_AVAILABILITY_CHECK, CONF_AVAILABILITY_RETRY_LIMIT, CONF_OPERATOR_ID, CONF_AIRCO_ID, DOMAIN
from .const import CONF_AVAILABILITY_CHECK, CONF_AVAILABILITY_RETRY_LIMIT, CONF_OPERATOR_ID, CONF_AIRCO_ID, DOMAIN, \
CONF_CREATE_SWING_MODE_SELECT
from .wfrac.repository import Repository

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -52,7 +53,7 @@ def _find_entry_matching_option(self, key, matches):
return None

async def _async_register_airco(
self, hass: HomeAssistant, data: dict
self, hass: HomeAssistant, data: dict
) -> dict[str, Any]:
"""Validate the user input allows us to connect, and register with the airco device"""
if len(data[CONF_HOST]) < 3:
Expand Down Expand Up @@ -114,11 +115,11 @@ async def _async_fetch_device_id(self):
return f"homeassistant-device-{uuid4().hex[21:]}"

async def _async_create_common(
self,
step_id: str,
data_schema: vol.Schema,
user_input: dict[str, Any] | None = None,
description_placeholders: dict[str, str] | None = None,
self,
step_id: str,
data_schema: vol.Schema,
user_input: dict[str, Any] | None = None,
description_placeholders: dict[str, str] | None = None,
):
"""Create a new entry"""
errors = {}
Expand Down Expand Up @@ -206,7 +207,7 @@ async def async_step_discovery_confirm(self, user_input=None):
@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
"""Create the options flow."""
return WfRacOptionsFlowHandler(config_entry)
Expand All @@ -221,6 +222,7 @@ async def async_step_user(self, user_input=None):
field(CONF_HOST, vol.Required): cv.string,
field(CONF_PORT, vol.Optional, 51443): cv.port,
field(CONF_FORCE_UPDATE, vol.Optional, False): cv.boolean,
field(CONF_CREATE_SWING_MODE_SELECT, vol.Optional, True): cv.boolean,
}
)

Expand All @@ -229,7 +231,7 @@ async def async_step_user(self, user_input=None):
)

async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
"""Handle zeroconf discovery."""

Expand Down Expand Up @@ -273,8 +275,8 @@ def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
self.config_entry = config_entry

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> config_entries.ConfigFlowResult:
self, user_input: dict[str, Any] | None = None
):
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
Expand All @@ -289,11 +291,11 @@ async def async_step_init(
): str,
vol.Required(
CONF_AVAILABILITY_CHECK,
default=self.config_entry.options.get(CONF_AVAILABILITY_CHECK, True ), # type: ignore
default=self.config_entry.options.get(CONF_AVAILABILITY_CHECK, True), # type: ignore
): bool,
vol.Optional(
CONF_AVAILABILITY_RETRY_LIMIT,
default=self.config_entry.options.get( CONF_AVAILABILITY_RETRY_LIMIT, 3 ), # type: ignore
default=self.config_entry.options.get(CONF_AVAILABILITY_RETRY_LIMIT, 3), # type: ignore
): int,
},
),
Expand Down
7 changes: 5 additions & 2 deletions custom_components/mitsubishi_wf_rac/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
CONF_AIRCO_ID = "airco_id"
CONF_AVAILABILITY_CHECK = "availability_check"
CONF_AVAILABILITY_RETRY_LIMIT = "availability_retry_limit"
CONF_CREATE_SWING_MODE_SELECT = "create_swing_mode_select"
ATTR_DEVICE_ID = "device_id"
ATTR_CONNECTED_ACCOUNTS = "connected_accounts"

Expand Down Expand Up @@ -44,6 +45,7 @@

SUPPORT_FLAGS = (
ClimateEntityFeature.FAN_MODE
| ClimateEntityFeature.SWING_HORIZONTAL_MODE
| ClimateEntityFeature.SWING_MODE
| ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.TURN_OFF
Expand Down Expand Up @@ -101,7 +103,7 @@
SWING_3D_AUTO,
]

HORIZONTAL_SWING_MODE_TRANSLATION = {
SWING_HORIZONTAL_MODE_TRANSLATION = {
SWING_HORIZONTAL_AUTO: 0,
SWING_HORIZONTAL_POSITION_1: 1,
SWING_HORIZONTAL_POSITION_2: 2,
Expand All @@ -112,7 +114,7 @@
SWING_HORIZONTAL_POSITION_7: 7,
}

SUPPORT_HORIZONTAL_SWING_MODES = [
SUPPORT_SWING_HORIZONTAL_MODES = [
SWING_HORIZONTAL_AUTO,
SWING_HORIZONTAL_POSITION_1,
SWING_HORIZONTAL_POSITION_2,
Expand All @@ -121,6 +123,7 @@
SWING_HORIZONTAL_POSITION_5,
SWING_HORIZONTAL_POSITION_6,
SWING_HORIZONTAL_POSITION_7,
SWING_3D_AUTO,
]


Expand Down
Loading

0 comments on commit 4d27717

Please sign in to comment.