diff --git a/changelog b/changelog index 01a21a3..8bb5d67 100644 --- a/changelog +++ b/changelog @@ -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: diff --git a/custom_components/virtual/__init__.py b/custom_components/virtual/__init__.py index 090478f..97b62bc 100644 --- a/custom_components/virtual/__init__.py +++ b/custom_components/virtual/__init__.py @@ -30,7 +30,7 @@ from .cfg import BlendedCfg, UpgradeCfg -__version__ = '0.9.0b15' +__version__ = '0.9.0b16' _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/virtual/fan.py b/custom_components/virtual/fan.py index cbb9647..4b43dc7 100644 --- a/custom_components/virtual/fan.py +++ b/custom_components/virtual/fan.py @@ -7,6 +7,8 @@ from __future__ import annotations import logging +from typing import Any + import voluptuous as vol from collections.abc import Callable @@ -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), @@ -144,16 +146,12 @@ 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 @@ -161,35 +159,44 @@ def set_preset_mode(self, preset_mode: str) -> None: 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 diff --git a/custom_components/virtual/light.py b/custom_components/virtual/light.py index 39489a6..5ca5efb 100644 --- a/custom_components/virtual/light.py +++ b/custom_components/virtual/light.py @@ -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 ( @@ -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) @@ -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 diff --git a/custom_components/virtual/lock.py b/custom_components/virtual/lock.py index 6307ffa..f6cc18d 100644 --- a/custom_components/virtual/lock.py +++ b/custom_components/virtual/lock.py @@ -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 @@ -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() @@ -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() diff --git a/custom_components/virtual/manifest.json b/custom_components/virtual/manifest.json index 8acb6cb..f4c37c1 100644 --- a/custom_components/virtual/manifest.json +++ b/custom_components/virtual/manifest.json @@ -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" } diff --git a/custom_components/virtual/switch.py b/custom_components/virtual/switch.py index 5c0ac66..1fdfeb9 100644 --- a/custom_components/virtual/switch.py +++ b/custom_components/virtual/switch.py @@ -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