diff --git a/changelog b/changelog index d3b2fe13..8c141036 100644 --- a/changelog +++ b/changelog @@ -1,4 +1,7 @@ aarlo +0.8.1.4 + Fix IO in event loop issue. + Possible light warning fix. 0.8.1.3 Fix missing motion event for all-in-one sensor 0.8.1.2 diff --git a/custom_components/aarlo/__init__.py b/custom_components/aarlo/__init__.py index 8acf3e2e..766bfcd7 100644 --- a/custom_components/aarlo/__init__.py +++ b/custom_components/aarlo/__init__.py @@ -53,7 +53,7 @@ from .cfg import BlendedCfg, PyaarloCfg -__version__ = "0.8.1.3" +__version__ = "0.8.1.4" _LOGGER = logging.getLogger(__name__) @@ -237,7 +237,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: _LOGGER.debug(f'async setup for aarlo') # Get the blended config. - cfg = BlendedCfg(hass, entry.data, entry.options) + cfg = BlendedCfg(hass) + await cfg.async_load_and_merge(entry.data, entry.options) domain_config = cfg.domain_config injection_service = domain_config.get(CONF_INJECTION_SERVICE, False) diff --git a/custom_components/aarlo/cfg.py b/custom_components/aarlo/cfg.py index e8374c1c..8749dea6 100644 --- a/custom_components/aarlo/cfg.py +++ b/custom_components/aarlo/cfg.py @@ -14,6 +14,7 @@ layout. """ +import aiofiles import copy import logging @@ -33,7 +34,7 @@ UnitOfTemperature ) from homeassistant.helpers import config_validation as cv -from homeassistant.util.yaml import load_yaml, save_yaml +from homeassistant.util.yaml import parse_yaml, dump from pyaarlo.constant import ( AIR_QUALITY_KEY, @@ -283,6 +284,29 @@ def _default_config_file(hass) -> str: return hass.config.path("aarlo.yaml") +async def _async_load_yaml(file_name): + _LOGGER.debug("_async_load_yaml1 file_name for %s", file_name) + try: + async with aiofiles.open(file_name, 'r') as yaml_file: + _LOGGER.debug("_async_load_yaml2 file_name for %s", file_name) + contents = await yaml_file.read() + _LOGGER.debug("_async_load_yaml3 file_name for %s", file_name) + return parse_yaml(contents) + except Exception as e: + _LOGGER.debug("_async_load_yaml3 file_name for %s", file_name) + return {} + + +async def _async_save_yaml(file_name, data): + _LOGGER.debug("_async_save_yaml1 file_name for %s", file_name) + try: + async with aiofiles.open(file_name, 'w') as yaml_file: + data = dump(data) + await yaml_file.write(data) + except Exception as e: + _LOGGER.debug("_async_load_yaml3 file_name for %s", file_name) + + def _fix_config(config): """Find and return the aarlo entry from any platform config. """ @@ -321,7 +345,7 @@ class BlendedCfg(object): them with flow data and options. """ - def __init__(self, hass, data, options): + def __init__(self, hass): self._hass = hass self._main_config = {} self._alarm_config = {} @@ -329,18 +353,11 @@ def __init__(self, hass, data, options): self._sensor_config = {} self._switch_config = {} - self._load() - self._merge(data, options) - - def _load(self): + async def _async_load(self): """Load extra config from aarlo.yaml file.""" # Read in current config - config = {} - try: - config = load_yaml(_default_config_file(self._hass)) - except Exception as e: - _LOGGER.debug(f"failed to read aarlo config {str(e)}") + config = await _async_load_yaml(_default_config_file(self._hass)) # Bring in all the defaults then overwrite them. I'm trying to decouple # the pyaarlo config from this config. @@ -368,6 +385,10 @@ def _merge(self, data, options): _LOGGER.debug(f"m-sensor-config={self._sensor_config}") _LOGGER.debug(f"m-switch-config={self._switch_config}") + async def async_load_and_merge(self, data, options): + await self._async_load() + self._merge(data, options) + @property def domain_config(self): return self._main_config @@ -394,7 +415,7 @@ class UpgradeCfg(object): """ @staticmethod - def create_file_config(hass, config): + async def create_file_config(hass, config): """ Take the current aarlo config and make the new yaml file. Aarlo seems to need a lot of fine tuning so rather than get rid of @@ -423,13 +444,10 @@ def create_file_config(hass, config): # to move any into the file we can read it from here. # Save it out. - try: - save_yaml(_default_config_file(hass), { - "version": 1, - COMPONENT_DOMAIN: file_config, - }) - except Exception as e: - _LOGGER.debug(f"couldn't save user data {str(e)}") + await _async_save_yaml(_default_config_file(hass), { + "version": 1, + COMPONENT_DOMAIN: file_config, + }) @staticmethod def create_flow_data(config): diff --git a/custom_components/aarlo/config_flow.py b/custom_components/aarlo/config_flow.py index 8af2da6c..be870684 100644 --- a/custom_components/aarlo/config_flow.py +++ b/custom_components/aarlo/config_flow.py @@ -161,7 +161,7 @@ async def async_step_import(self, import_data): """Import momentary config from configuration.yaml.""" _LOGGER.info("importing aarlo YAML") - UpgradeCfg.create_file_config(self.hass, import_data) + await UpgradeCfg.create_file_config(self.hass, import_data) data = UpgradeCfg.create_flow_data(import_data) options = UpgradeCfg.create_flow_options(import_data) diff --git a/custom_components/aarlo/light.py b/custom_components/aarlo/light.py index a47da2f6..e336ff36 100644 --- a/custom_components/aarlo/light.py +++ b/custom_components/aarlo/light.py @@ -105,6 +105,7 @@ def __init__(self, light, aarlo_config): self._attr_brightness = None self._attr_is_on = False self._attr_should_poll = False + self._attr_color_mode = ColorMode.BRIGHTNESS self._attr_supported_color_modes = { ColorMode.BRIGHTNESS } @@ -189,8 +190,9 @@ def __init__(self, camera, aarlo_config): self._attr_hs_color = None self._attr_max_mireds = color_util.color_temperature_kelvin_to_mired(9000) self._attr_min_mireds = color_util.color_temperature_kelvin_to_mired(2500) + self._attr_color_mode = ColorMode.COLOR_TEMP self._attr_supported_color_modes = { - ColorMode.BRIGHTNESS, ColorMode.COLOR_TEMP, ColorMode.HS + ColorMode.COLOR_TEMP, ColorMode.HS } self._attr_supported_features = LightEntityFeature( LightEntityFeature.EFFECT @@ -216,12 +218,15 @@ def _set_light_mode(self, light_mode): rgb.get("red"), rgb.get("green"), rgb.get("blue") ) self._attr_effect = LIGHT_EFFECT_NONE + self._attr_color_mode = ColorMode.HS elif mode == "temperature": temperature = light_mode.get("temperature") self._attr_color_temp = color_util.color_temperature_kelvin_to_mired(temperature) self._attr_hs_color = color_util.color_temperature_to_hs(temperature) self._attr_effect = LIGHT_EFFECT_NONE + self._attr_color_mode = ColorMode.COLOR_TEMP elif mode == LIGHT_EFFECT_RAINBOW: + self._attr_color_mode = ColorMode.HS self._attr_effect = LIGHT_EFFECT_RAINBOW async def async_added_to_hass(self): @@ -285,6 +290,7 @@ def __init__(self, camera, aarlo_config): self._sleep_time_rel = None self._attr_brightness = None + self._attr_color_mode = ColorMode.BRIGHTNESS self._attr_supported_color_modes = { ColorMode.BRIGHTNESS } @@ -368,6 +374,7 @@ def __init__(self, camera, aarlo_config): self._attr_brightness = None self._attr_effect = None + self._attr_color_mode = ColorMode.BRIGHTNESS self._attr_supported_color_modes = { ColorMode.BRIGHTNESS } diff --git a/custom_components/aarlo/manifest.json b/custom_components/aarlo/manifest.json index adf7cbc0..235d1546 100644 --- a/custom_components/aarlo/manifest.json +++ b/custom_components/aarlo/manifest.json @@ -15,5 +15,5 @@ "unidecode", "pyaarlo>=0.8.0.7" ], - "version": "0.8.1.3" + "version": "0.8.1.4" }