Skip to content

Commit

Permalink
Make async (#125)
Browse files Browse the repository at this point in the history
* Use async interface where appropriate.

* Use async interface where appropriate.
  • Loading branch information
twrecked authored Aug 19, 2024
1 parent 7208097 commit 8ba382e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 32 deletions.
2 changes: 2 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.9.0b16:
Use async interfaces where possible.
0.9.0b15:
Fix threading issue with cover and valve.
0.9.0b14:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/virtual/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from .cfg import BlendedCfg, UpgradeCfg


__version__ = '0.9.0b15'
__version__ = '0.9.0b16'

_LOGGER = logging.getLogger(__name__)

Expand Down
45 changes: 26 additions & 19 deletions custom_components/virtual/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from __future__ import annotations

import logging
from typing import Any

import voluptuous as vol
from collections.abc import Callable

Expand Down Expand Up @@ -134,7 +136,7 @@ def _restore_state(self, state, config):
self._attr_preset_mode = state.attributes.get(ATTR_PRESET_MODE)

def _update_attributes(self):
super()._update_attributes();
super()._update_attributes()
self._attr_extra_state_attributes.update({
name: value for name, value in (
(ATTR_DIRECTION, self._attr_current_direction),
Expand All @@ -144,52 +146,57 @@ def _update_attributes(self):
) if value is not None
})

def set_percentage(self, percentage: int) -> None:
"""Set the speed of the fan, as a percentage."""
_LOGGER.debug(f"setting {self.name} pcent to {percentage}")
def _set_percentage(self, percentage: int) -> None:
self._attr_percentage = percentage
self._attr_preset_mode = None
self._update_attributes()

def set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
_LOGGER.debug(f"setting {self.name} mode to {preset_mode}")
def _set_preset_mode(self, preset_mode: str) -> None:
if preset_mode in self.preset_modes:
self._attr_preset_mode = preset_mode
self._attr_percentage = None
self._update_attributes()
else:
raise ValueError(f"Invalid preset mode: {preset_mode}")

def turn_on(
self,
speed: str = None,
percentage: int = None,
preset_mode: str = None,
**kwargs,
async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed of the fan, as a percentage."""
_LOGGER.debug(f"setting {self.name} pcent to {percentage}")
self._set_percentage(percentage)

async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
_LOGGER.debug(f"setting {self.name} mode to {preset_mode}")
self._set_preset_mode(preset_mode)

async def async_turn_on(
self,
percentage: int | None = None,
preset_mode: str | None = None,
**kwargs: Any,
) -> None:
"""Turn on the entity."""
_LOGGER.debug(f"turning {self.name} on")
if preset_mode:
self.set_preset_mode(preset_mode)
self._set_preset_mode(preset_mode)
return

if percentage is None:
percentage = 67
self.set_percentage(percentage)
self._set_percentage(percentage)

def turn_off(self, **kwargs) -> None:
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the entity."""
_LOGGER.debug(f"turning {self.name} off")
self.set_percentage(0)
self._set_percentage(0)

def set_direction(self, direction: str) -> None:
async def async_set_direction(self, direction: str) -> None:
"""Set the direction of the fan."""
_LOGGER.debug(f"setting direction of {self.name} to {direction}")
self._attr_current_direction = direction
self._update_attributes()

def oscillate(self, oscillating: bool) -> None:
async def async_oscillate(self, oscillating: bool) -> None:
"""Set oscillation."""
_LOGGER.debug(f"setting oscillate of {self.name} to {oscillating}")
self._attr_oscillating = oscillating
Expand Down
5 changes: 3 additions & 2 deletions custom_components/virtual/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pprint
import voluptuous as vol
from collections.abc import Callable
from typing import Any

import homeassistant.helpers.config_validation as cv
from homeassistant.components.light import (
Expand Down Expand Up @@ -186,7 +187,7 @@ def _update_attributes(self):
) if value is not None
})

def turn_on(self, **kwargs):
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the light on."""
_LOGGER.debug(f"turning {self.name} on {pprint.pformat(kwargs)}")
hs_color = kwargs.get(ATTR_HS_COLOR, None)
Expand All @@ -210,7 +211,7 @@ def turn_on(self, **kwargs):
self._attr_is_on = True
self._update_attributes()

def turn_off(self, **kwargs):
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the light off."""
_LOGGER.debug(f"turning {self.name} off {pprint.pformat(kwargs)}")
self._attr_is_on = False
Expand Down
14 changes: 7 additions & 7 deletions custom_components/virtual/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
from typing import Any

import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util
from homeassistant.components.lock import (
DOMAIN as PLATFORM_DOMAIN,
LockEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_LOCKED
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import track_point_in_time
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

from . import get_entity_configs
Expand Down Expand Up @@ -136,6 +135,7 @@ def _jam(self) -> None:
self._attr_is_locked = False
self._attr_is_jammed = True

@callback
async def _finish_operation(self, _point_in_time) -> None:
if self.is_locking:
self._lock()
Expand All @@ -144,22 +144,22 @@ async def _finish_operation(self, _point_in_time) -> None:
self.async_schedule_update_ha_state()

def _start_operation(self):
track_point_in_time(self._hass, self._finish_operation, dt_util.utcnow() + self._change_time)
async_call_later(self.hass, self._change_time, self._finish_operation)

def lock(self, **kwargs: Any) -> None:
async def async_lock(self, **kwargs: Any) -> None:
if self._change_time == DEFAULT_CHANGE_TIME:
self._lock()
else:
self._locking()
self._start_operation()

def unlock(self, **kwargs: Any) -> None:
async def async_unlock(self, **kwargs: Any) -> None:
if self._change_time == DEFAULT_CHANGE_TIME:
self._unlock()
else:
self._unlocking()
self._start_operation()

def open(self, **kwargs: Any) -> None:
async def async_open(self, **kwargs: Any) -> None:
_LOGGER.debug(f"opening {self.name}")
self.unlock()
2 changes: 1 addition & 1 deletion custom_components/virtual/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"documentation": "https://github.com/twrecked/hass-virtual/blob/master/README.md",
"iot_class": "local_push",
"issue_tracker": "https://github.com/twrecked/hass-virtual/issues",
"version": "0.9.0b15"
"version": "0.9.0b16"
}
4 changes: 2 additions & 2 deletions custom_components/virtual/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ def _update_attributes(self):
) if value is not None
})

def turn_on(self, **kwargs: Any) -> None:
async def async_turn_on(self, **kwargs: Any) -> None:
_LOGGER.debug(f"turning {self.name} on")
self._attr_is_on = True

def turn_off(self, **kwargs: Any) -> None:
async def async_turn_off(self, **kwargs: Any) -> None:
_LOGGER.debug(f"turning {self.name} off")
self._attr_is_on = False

0 comments on commit 8ba382e

Please sign in to comment.