Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support toggling features #67

Merged
merged 1 commit into from
Jan 12, 2024
Merged
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
7 changes: 7 additions & 0 deletions custom_components/wellbeing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,13 @@ async def set_work_mode(self, pnc_id: str, mode: Mode):
result = await self._send_command(self._current_access_token, pnc_id, data)
_LOGGER.debug(f"Set Fan Speed: {result}")

async def set_feature_state(self, pnc_id: str, feature: str, state: bool):
"""Set the state of a feature (Ionizer, UILight, SafetyLock)."""
# Construct the command directly using the feature name
data = {feature: state}
await self._send_command(self._current_access_token, pnc_id, data)
_LOGGER.debug(f"Set {feature} State to {state}")

async def _send_command(self, access_token: str, pnc_id: str, command: dict) -> None:
"""Get data from the API."""
headers = {
Expand Down
3 changes: 2 additions & 1 deletion custom_components/wellbeing/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
SENSOR = "sensor"
SWITCH = "switch"
FAN = "fan"
PLATFORMS = [SENSOR, FAN, BINARY_SENSOR]
SWITCH = "switch"
PLATFORMS = [SENSOR, FAN, BINARY_SENSOR, SWITCH]

# Configuration and options
CONF_ENABLED = "enabled"
Expand Down
14 changes: 12 additions & 2 deletions custom_components/wellbeing/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,18 @@ async def async_set_preset_mode(self, preset_mode: str) -> None:
def is_on(self):
return self.preset_mode is not Mode.OFF

async def async_turn_on(self, speed: str = None, percentage: int = None,
preset_mode: str = None, **kwargs) -> None:
async def async_turn_on(self, speed: str = None, percentage: int = None, preset_mode: str = None, **kwargs) -> None:
self._preset_mode = Mode(preset_mode or Mode.AUTO.value)

# Handle incorrect percentage
if percentage is not None and isinstance(percentage, str):
try:
percentage = int(percentage)
except ValueError:
_LOGGER.error(f"Invalid percentage value: {percentage}")
return

# Proceed with the provided or default percentage
self._speed = math.floor(percentage_to_ranged_value(self._speed_range, percentage or 10))
self.get_appliance.clear_mode()
self.get_entity.clear_state()
Expand All @@ -127,6 +136,7 @@ async def async_turn_on(self, speed: str = None, percentage: int = None,
await asyncio.sleep(10)
await self.coordinator.async_request_refresh()


async def async_turn_off(self, **kwargs) -> None:
"""Turn off the entity."""
self._preset_mode = Mode.OFF
Expand Down
45 changes: 45 additions & 0 deletions custom_components/wellbeing/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Switch platform for Wellbeing."""
from homeassistant.components.switch import SwitchEntity
from .const import DOMAIN
from .entity import WellbeingEntity

async def async_setup_entry(hass, entry, async_add_devices):
"""Setup switch platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
appliances = coordinator.data.get('appliances', None)

if appliances is not None:
for pnc_id, appliance in appliances.appliances.items():
# Assuming that the appliance supports these features
async_add_devices([
WellbeingSwitch(coordinator, entry, pnc_id, "Ionizer"),
WellbeingSwitch(coordinator, entry, pnc_id, "UILight"),
WellbeingSwitch(coordinator, entry, pnc_id, "SafetyLock"),
])

class WellbeingSwitch(WellbeingEntity, SwitchEntity):
"""Wellbeing Switch class."""

def __init__(self, coordinator, config_entry, pnc_id, function):
super().__init__(coordinator, config_entry, pnc_id, "binary_sensor", function)
self._function = function
self._is_on = self.get_entity.state

@property
def is_on(self):
"""Return true if switch is on."""
return self._is_on

async def async_turn_on(self, **kwargs):
"""Turn the switch on."""
await self.coordinator.api.set_feature_state(self.pnc_id, self._function, True)
self._is_on = True
self.async_write_ha_state()
await self.coordinator.async_request_refresh()

async def async_turn_off(self, **kwargs):
"""Turn the switch off."""
await self.coordinator.api.set_feature_state(self.pnc_id, self._function, False)
self._is_on = False
self.async_write_ha_state()
await self.coordinator.async_request_refresh()
7 changes: 5 additions & 2 deletions custom_components/wellbeing/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"password": "Password"
},
"description": "Enter the password for {username}.",
"title": "Reauthenticate an August account"
"title": "Reauthenticate an Electrolux account"
}
},
"error": {
Expand All @@ -27,7 +27,10 @@
"step": {
"user": {
"data": {
"scan_interval": "API update interval (seconds)"
"scan_interval": "API update interval (seconds)",
"binary_sensor": "Binary sensor activated",
"sensor": "Sensor activated",
"switch": "Switch activated"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/wellbeing/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"password": "Password"
},
"description": "Enter the password for {username}.",
"title": "Reauthenticate an August account"
"title": "Reauthenticate an Electrolux account"
}
},
"error": {
Expand Down
2 changes: 1 addition & 1 deletion custom_components/wellbeing/translations/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"password": "Password"
},
"description": "Enter the password for {username}.",
"title": "Reauthenticate an August account"
"title": "Reauthenticate an Electrolux account"
}
},
"error": {
Expand Down
2 changes: 1 addition & 1 deletion custom_components/wellbeing/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"password": "Password"
},
"description": "Enter the password for {username}.",
"title": "Reauthenticate an August account"
"title": "Reauthenticate an Electrolux account"
}
},
"error": {
Expand Down
38 changes: 38 additions & 0 deletions custom_components/wellbeing/translations/se.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"config": {
"step": {
"user": {
"description": "Om du behöver hjälp med konfigurationen, se här: https://github.com/JohNan/homeassistant-wellbeing",
"data": {
"username": "Användarnamn",
"password": "Lösenord"
}
},
"reauth_validate": {
"data": {
"password": "Lösenord"
},
"description": "Ange lösenord för {username}.",
"title": "Återautentisera Electrolux-kontot"
}
},
"error": {
"auth": "Användarnamn eller lösenord är felaktigt."
},
"abort": {
"single_instance_allowed": "Endast en instans är tillåten."
}
},
"options": {
"step": {
"user": {
"data": {
"scan_interval": "API-uppdateringsintervall (sekunder)",
"binary_sensor": "Binär sensor aktiverad",
"sensor": "Sensor aktiverad",
"switch": "Brytare aktiverad"
}
}
}
}
}
Loading