Skip to content

Commit

Permalink
Misc tidies (#969)
Browse files Browse the repository at this point in the history
* Fix a light error.

* Remove an event io error.
  • Loading branch information
twrecked authored Jul 4, 2024
1 parent aee322c commit 3c94623
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 24 deletions.
3 changes: 3 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions custom_components/aarlo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
from .cfg import BlendedCfg, PyaarloCfg


__version__ = "0.8.1.3"
__version__ = "0.8.1.4"

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -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)

Expand Down
56 changes: 37 additions & 19 deletions custom_components/aarlo/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
layout.
"""

import aiofiles
import copy
import logging

Expand All @@ -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,
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -321,26 +345,19 @@ 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 = {}
self._binary_sensor_config = {}
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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/aarlo/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 8 additions & 1 deletion custom_components/aarlo/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/aarlo/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"unidecode",
"pyaarlo>=0.8.0.7"
],
"version": "0.8.1.3"
"version": "0.8.1.4"
}

0 comments on commit 3c94623

Please sign in to comment.