diff --git a/custom_components/fujitsu_airstage/climate.py b/custom_components/fujitsu_airstage/climate.py index 491c77d..1df4b4b 100644 --- a/custom_components/fujitsu_airstage/climate.py +++ b/custom_components/fujitsu_airstage/climate.py @@ -32,6 +32,7 @@ VERTICAL_LOWEST, VERTICAL_SWING, MINIMUM_HEAT, + CONF_TURN_ON_BEFORE_SET_TEMP, ) HA_STATE_TO_FUJITSU = { @@ -101,15 +102,21 @@ async def async_setup_entry( config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: + config_entry.async_on_unload(config_entry.add_update_listener(update_listener)) + instance: AirstageData = hass.data[AIRSTAGE_DOMAIN][config_entry.entry_id] entities: list[ClimateEntity] = [] if devices := instance.coordinator.data: for ac_key in devices: - entities.append(AirstageAC(instance, ac_key)) + entities.append(AirstageAC(instance, ac_key, config_entry)) async_add_entities(entities) +async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None: + """Update listener.""" + await hass.config_entries.async_reload(config_entry.entry_id) + class AirstageAC(AirstageAcEntity, ClimateEntity): """Airstage unit.""" @@ -122,6 +129,7 @@ class AirstageAC(AirstageAcEntity, ClimateEntity): _attr_max_temp = 32 _attr_min_temp = 16 _attr_name = None + _turn_on_before_set_temp = False _attr_fan_modes = [FAN_QUIET, FAN_LOW, FAN_MEDIUM, FAN_HIGH, FAN_AUTO] @@ -134,9 +142,10 @@ class AirstageAC(AirstageAcEntity, ClimateEntity): HVACMode.AUTO, ] - def __init__(self, instance: AirstageData, ac_key: str) -> None: + def __init__(self, instance: AirstageData, ac_key: str, config_entry: ConfigEntry) -> None: """Initialize an AdvantageAir AC unit.""" super().__init__(instance, ac_key) + self._turn_on_before_set_temp = config_entry.options.get(CONF_TURN_ON_BEFORE_SET_TEMP, False) @property def target_temperature(self) -> float | None: @@ -152,6 +161,8 @@ def target_temperature(self) -> float | None: async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperature.""" + if self._turn_on_before_set_temp and self.hvac_mode == HVACMode.OFF: + await self.async_turn_on() if self.hvac_mode != HVACMode.FAN_ONLY: await self._ac.set_target_temperature(kwargs.get(ATTR_TEMPERATURE)) diff --git a/custom_components/fujitsu_airstage/config_flow.py b/custom_components/fujitsu_airstage/config_flow.py index 6912b75..8483dfe 100644 --- a/custom_components/fujitsu_airstage/config_flow.py +++ b/custom_components/fujitsu_airstage/config_flow.py @@ -9,6 +9,7 @@ from homeassistant import config_entries from homeassistant.core import HomeAssistant +from homeassistant.core import callback from homeassistant.data_entry_flow import FlowResult from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -33,10 +34,40 @@ CONF_SELECT_POLLING, CONF_SELECT_POLLING_DESCRIPTION, DOMAIN, + CONF_TURN_ON_BEFORE_SET_TEMP, ) _LOGGER = logging.getLogger(__name__) +class OptionsFlow(config_entries.OptionsFlow): + """Handle a options flow for Airstage Fujitsu.""" + + def __init__(self, config_entry: config_entries.ConfigEntry): + """Initialize the options flow.""" + self._config_entry = config_entry + + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: + """Manage the options.""" + if user_input is not None: + return self.async_create_entry(data=user_input) + + options_schema = { + vol.Optional( + CONF_TURN_ON_BEFORE_SET_TEMP, + default=self._config_entry.options.get( + CONF_TURN_ON_BEFORE_SET_TEMP, + False + ), + ): bool, + } + + + return self.async_show_form( + step_id="init", + data_schema=vol.Schema(options_schema), + ) class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for Airstage Fujitsu.""" @@ -50,6 +81,15 @@ def __init__(self) -> None: self.country = None self.device_id = None self.ip_address = None + self.turn_on_before_set_temp = False + + @staticmethod + @callback + def async_get_options_flow( + config_entry: config_entries.ConfigEntry, + ) -> OptionsFlow: + """Get the Frigate Options flow.""" + return OptionsFlow(config_entry) async def async_step_details( self, user_input: dict[str, Any] | None = None diff --git a/custom_components/fujitsu_airstage/const.py b/custom_components/fujitsu_airstage/const.py index 989d992..b2a2e78 100644 --- a/custom_components/fujitsu_airstage/const.py +++ b/custom_components/fujitsu_airstage/const.py @@ -25,3 +25,4 @@ CONF_CLOUD = "cloud" CONF_SELECT_POLLING = "select_polling" CONF_SELECT_POLLING_DESCRIPTION = "select_polling_desc" +CONF_TURN_ON_BEFORE_SET_TEMP = "conf_turn_on_before_set_temp" diff --git a/custom_components/fujitsu_airstage/strings.json b/custom_components/fujitsu_airstage/strings.json index c217cbb..4c0c406 100644 --- a/custom_components/fujitsu_airstage/strings.json +++ b/custom_components/fujitsu_airstage/strings.json @@ -1,4 +1,11 @@ { + "options": { + "step": { + "init": { + "conf_turn_on_before_set_temp": "[%key:common::config_flow::options::conf_turn_on_before_set_temp]" + } + } + }, "config": { "step": { "details": { @@ -35,4 +42,4 @@ "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" } } -} \ No newline at end of file +} diff --git a/custom_components/fujitsu_airstage/translations/en.json b/custom_components/fujitsu_airstage/translations/en.json index 5c667e2..6d3cfe4 100644 --- a/custom_components/fujitsu_airstage/translations/en.json +++ b/custom_components/fujitsu_airstage/translations/en.json @@ -1,4 +1,13 @@ { + "options": { + "step": { + "init": { + "data": { + "conf_turn_on_before_set_temp": "Turn on device before setting temperature" + } + } + } + }, "config": { "abort": { "already_configured": "Device is already configured" @@ -33,4 +42,4 @@ } } } -} \ No newline at end of file +}