Skip to content

Commit

Permalink
Fan changed to preset modes
Browse files Browse the repository at this point in the history
Signed-off-by: Siddhu <[email protected]>
  • Loading branch information
arevindh committed Feb 22, 2023
1 parent 75dbeb9 commit 25afd39
Showing 1 changed file with 30 additions and 35 deletions.
65 changes: 30 additions & 35 deletions custom_components/tinxy/fan.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Example integration using DataUpdateCoordinator."""
"""Tinxy Fan Entity."""
import logging
from typing import Any, Optional
from typing import Any

from homeassistant.components.fan import FanEntity, SUPPORT_SET_SPEED
from homeassistant.components.fan import (
FanEntity,
FanEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util.percentage import (
ordered_list_item_to_percentage,
percentage_to_ordered_list_item,
)
from homeassistant.helpers.entity import DeviceInfo

from .const import DOMAIN

Expand Down Expand Up @@ -58,11 +58,6 @@ def __init__(self, coordinator, apidata, idx) -> None:
self.idx = idx
self.coordinator = coordinator
self.api = apidata
# _LOGGER.warning(
# self.coordinator.data[self.idx]["name"]
# + " - "
# + self.coordinator.data[self.idx]["state"]
# )

@callback
def _handle_coordinator_update(self) -> None:
Expand Down Expand Up @@ -98,27 +93,27 @@ def available(self) -> bool:
return True if self.coordinator.data[self.idx]["status"] == 1 else False

@property
def device_info(self):
def device_info(self) -> DeviceInfo:
return self.coordinator.data[self.idx]["device"]

@property
def supported_features(self):
return SUPPORT_SET_SPEED
def preset_modes(self) -> list[str] | None:
"""List all available preset modes"""
return ["Low", "Medium", "High"]

@property
def percentage(self) -> Optional[int]:
"""Return the current speed percentage."""
return self.coordinator.data[self.idx]["brightness"]
def supported_features(self) -> FanEntityFeature:
"""List all supported features"""
return FanEntityFeature.PRESET_MODE

async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
# self._is_on = True
await self.api.set_device_state(
self.coordinator.data[self.idx]["device_id"],
str(self.coordinator.data[self.idx]["relay_no"]),
1,
)
await self.coordinator.async_request_refresh()
@property
def preset_mode(self) -> str | None:
"""Get current preset mode"""
if self.coordinator.data[self.idx]["brightness"] == 100:
return "High"
elif self.coordinator.data[self.idx]["brightness"] == 66:
return "Medium"
return "Low"

async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off."""
Expand All @@ -130,20 +125,20 @@ async def async_turn_off(self, **kwargs: Any) -> None:
)
await self.coordinator.async_request_refresh()

async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed percentage of the fan."""
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode of the fan."""

if percentage > 1 and percentage <= 33:
percentage = 33
elif percentage > 33 and percentage <= 66:
percentage = 66
elif percentage > 66:
if preset_mode == "High":
percentage = 100
elif preset_mode == "Medium":
percentage = 66
else:
percentage = 33

await self.api.set_device_state(
self.coordinator.data[self.idx]["device_id"],
str(self.coordinator.data[self.idx]["relay_no"]),
1,
percentage,
)
await self.coordinator.async_request_refresh()
await self.coordinator.async_refresh()

0 comments on commit 25afd39

Please sign in to comment.