Skip to content

Commit

Permalink
Added preset mode (#13)
Browse files Browse the repository at this point in the history
* Added preset mode

* bump version
  • Loading branch information
hmn authored Nov 8, 2022
1 parent eb5266e commit 872a947
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 39 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

Integration for https://www.siku.at/produkte/ wifi fans

Tested on "SIKU RV 50 W Pro WIFI v1"

## Installation

1. Using the tool of choice open the directory (folder) for your HA configuration (where you find `configuration.yaml`).
Expand Down
2 changes: 2 additions & 0 deletions custom_components/siku/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ async def direction(self, direction: str) -> None:

async def sleep(self) -> None:
"""Set fan to sleep mode"""
await self.power_on()
hexlist = await self._send_command(COMMAND_SLEEP)
return await self._translate_response(hexlist)

async def party(self) -> None:
"""Set fan to party mode"""
await self.power_on()
hexlist = await self._send_command(COMMAND_PARTY)
return await self._translate_response(hexlist)

Expand Down
91 changes: 54 additions & 37 deletions custom_components/siku/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@
from .const import DOMAIN
from .coordinator import SikuDataUpdateCoordinator

# from .api import SikuApi

LOGGER = logging.getLogger(__name__)

# percentage = ordered_list_item_to_percentage(FAN_SPEEDS, "01")
# named_speed = percentage_to_ordered_list_item(FAN_SPEEDS, 33)

PRESET_MODE_AUTO = "auto"

PRESET_MODE_AUTO = "auto"
PRESET_MODE_SMART = "smart"
PRESET_MODE_SLEEP = "sleep"
PRESET_MODE_ON = "on"
PRESET_MODE_PARTY = "party"
PRESET_MODE_SLEEP = "sleep"


async def async_setup_entry(
Expand Down Expand Up @@ -59,7 +55,14 @@ class SikuFan(SikuEntity, FanEntity):
FanEntityFeature.SET_SPEED
| FanEntityFeature.OSCILLATE
| FanEntityFeature.DIRECTION
| FanEntityFeature.PRESET_MODE
)
_attr_preset_modes = [
PRESET_MODE_AUTO,
PRESET_MODE_ON,
PRESET_MODE_PARTY,
PRESET_MODE_SLEEP,
]
_attr_has_entity_name = True
_attr_name = None
_attr_should_poll = True
Expand All @@ -82,43 +85,37 @@ def unique_id(self) -> str:
"""Return the unique id."""
return self._unique_id

@property
def supported_features(self) -> int:
"""Flag supported features."""
return self._attr_supported_features

@property
def speed_count(self) -> int:
"""Return the number of speeds the fan supports."""
return len(FAN_SPEEDS)

@property
def percentage(self) -> int | None:
"""Return the current speed percentage."""
if hasattr(self, "_attr_percentage"):
return self._attr_percentage
return 0

def set_percentage(self, percentage: int) -> None:
"""Set the speed of the fan, as a percentage."""
self._attr_percentage = percentage
if percentage == 0:
self.set_preset_mode(None)

async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed of the fan, as a percentage."""
if percentage == 0:
await self.async_turn_off()
await self.coordinator.api.power_off()
await self.hass.async_add_executor_job(self.set_preset_mode, None)
else:
if not self.is_on:
await self.async_turn_on()
await self.coordinator.api.power_on()
await self.coordinator.api.speed(
percentage_to_ordered_list_item(FAN_SPEEDS, percentage)
)
if self.oscillating:
await self.hass.async_add_executor_job(
self.set_preset_mode, PRESET_MODE_AUTO
)
else:
await self.hass.async_add_executor_job(
self.set_preset_mode, PRESET_MODE_ON
)
await self.hass.async_add_executor_job(self.set_percentage, percentage)

@property
def oscillating(self) -> bool | None:
"""Oscillating."""
return self._attr_oscillating
self.async_write_ha_state()

def oscillate(self, oscillating: bool) -> None:
"""Oscillate the fan."""
Expand All @@ -133,26 +130,26 @@ async def async_oscillate(self, oscillating: bool) -> None:
if not self.is_on:
await self.async_turn_on()
await self.coordinator.api.direction("alternating")
await self.hass.async_add_executor_job(
self.set_preset_mode, PRESET_MODE_AUTO
)
else:
await self.coordinator.api.direction("forward")
await self.hass.async_add_executor_job(self.set_preset_mode, PRESET_MODE_ON)
await self.hass.async_add_executor_job(self.oscillate, oscillating)
self.async_write_ha_state()

@property
def current_direction(self) -> str | None:
"""Fan direction."""
return self._attr_current_direction

def set_direction(self, direction: str) -> None:
"""Set the direction of the fan."""
self._attr_current_direction = direction

async def async_set_direction(self, direction: str) -> None:
"""Set the direction of the fan."""
await self.coordinator.api.direction(direction)
self.set_direction(direction)
await self.hass.async_add_executor_job(self.set_direction, direction)
if self.oscillating:
self.oscillate(False)
await self.hass.async_add_executor_job(self.oscillate, False)
await self.hass.async_add_executor_job(self.set_preset_mode, PRESET_MODE_ON)
self.async_write_ha_state()

async def async_turn_on(
Expand All @@ -165,14 +162,34 @@ async def async_turn_on(
if percentage is None:
percentage = ordered_list_item_to_percentage(FAN_SPEEDS, FAN_SPEEDS[0])

await self.coordinator.api.power_on()
self.set_percentage(percentage)
await self.async_set_percentage(percentage)
self.async_write_ha_state()

async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the entity."""
await self.coordinator.api.power_off()
self.set_percentage(0)
await self.async_set_percentage(0)
self.async_write_ha_state()

def set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode of the fan."""
self._attr_preset_mode = preset_mode
self.schedule_update_ha_state()

async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
if preset_mode == PRESET_MODE_PARTY:
await self.async_turn_on()
await self.coordinator.api.party()
elif preset_mode == PRESET_MODE_SLEEP:
await self.async_turn_on()
await self.coordinator.api.sleep()
elif preset_mode == PRESET_MODE_AUTO:
await self.async_turn_on()
await self.async_oscillate(True)
elif preset_mode == PRESET_MODE_ON:
await self.async_turn_on()
await self.async_set_direction("forward")
await self.hass.async_add_executor_job(self.set_preset_mode, preset_mode)
self.async_write_ha_state()

async def async_added_to_hass(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/siku/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"dependencies": [],
"codeowners": ["@hmn"],
"iot_class": "local_polling",
"version": "1.0.1"
"version": "1.0.2"
}
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "Siku RV Fan integration",
"name": "Siku RV Fan",
"render_readme": true
}

0 comments on commit 872a947

Please sign in to comment.