Apply new customize format to Zwave (#5603)
This commit is contained in:
1 parent
405b2fdfa0
commit
1fb372ffdb
9 files changed
+171
-84
No files matched your search
@@ -1,25 +1,51 @@
|
||||
"""A helper module for customization."""
|
||||
import collections
|
||||
from typing import Dict, List
|
||||
from typing import Any, Dict, List
|
||||
import fnmatch
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import CONF_ENTITY_ID
|
||||
from homeassistant.core import HomeAssistant, split_entity_id
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
_OVERWRITE_KEY = 'overwrite'
|
||||
_OVERWRITE_CACHE_KEY = 'overwrite_cache'
|
||||
_OVERWRITE_KEY_FORMAT = '{}.overwrite'
|
||||
_OVERWRITE_CACHE_KEY_FORMAT = '{}.overwrite_cache'
|
||||
|
||||
_CUSTOMIZE_SCHEMA_ENTRY = vol.Schema({
|
||||
vol.Required(CONF_ENTITY_ID): vol.All(
|
||||
cv.ensure_list_csv, vol.Length(min=1), [vol.Schema(str)], [vol.Lower])
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
def set_customize(hass: HomeAssistant, customize: List[Dict]) -> None:
|
||||
def _convert_old_config(inp: Any) -> List:
|
||||
if not isinstance(inp, dict):
|
||||
return cv.ensure_list(inp)
|
||||
if CONF_ENTITY_ID in inp:
|
||||
return [inp] # sigle entry
|
||||
res = []
|
||||
|
||||
inp = vol.Schema({cv.match_all: dict})(inp)
|
||||
for key, val in inp.items():
|
||||
val = dict(val)
|
||||
val[CONF_ENTITY_ID] = key
|
||||
res.append(val)
|
||||
return res
|
||||
|
||||
|
||||
CUSTOMIZE_SCHEMA = vol.All(_convert_old_config, [_CUSTOMIZE_SCHEMA_ENTRY])
|
||||
|
||||
|
||||
def set_customize(
|
||||
hass: HomeAssistant, domain: str, customize: List[Dict]) -> None:
|
||||
"""Overwrite all current customize settings.
|
||||
|
||||
Async friendly.
|
||||
"""
|
||||
hass.data[_OVERWRITE_KEY] = customize
|
||||
hass.data[_OVERWRITE_CACHE_KEY] = {}
|
||||
hass.data[_OVERWRITE_KEY_FORMAT.format(domain)] = customize
|
||||
hass.data[_OVERWRITE_CACHE_KEY_FORMAT.format(domain)] = {}
|
||||
|
||||
|
||||
def get_overrides(hass: HomeAssistant, entity_id: str) -> Dict:
|
||||
def get_overrides(hass: HomeAssistant, domain: str, entity_id: str) -> Dict:
|
||||
"""Return a dictionary of overrides related to entity_id.
|
||||
|
||||
Whole-domain overrides are of lowest priorities,
|
||||
@@ -28,10 +54,11 @@ def get_overrides(hass: HomeAssistant, entity_id: str) -> Dict:
|
||||
|
||||
The lookups are cached.
|
||||
"""
|
||||
if _OVERWRITE_CACHE_KEY in hass.data and \
|
||||
entity_id in hass.data[_OVERWRITE_CACHE_KEY]:
|
||||
return hass.data[_OVERWRITE_CACHE_KEY][entity_id]
|
||||
if _OVERWRITE_KEY not in hass.data:
|
||||
cache_key = _OVERWRITE_CACHE_KEY_FORMAT.format(domain)
|
||||
if cache_key in hass.data and entity_id in hass.data[cache_key]:
|
||||
return hass.data[cache_key][entity_id]
|
||||
overwrite_key = _OVERWRITE_KEY_FORMAT.format(domain)
|
||||
if overwrite_key not in hass.data:
|
||||
return {}
|
||||
domain_result = {} # type: Dict[str, Any]
|
||||
glob_result = {} # type: Dict[str, Any]
|
||||
@@ -57,7 +84,7 @@ def get_overrides(hass: HomeAssistant, entity_id: str) -> Dict:
|
||||
else:
|
||||
target[key] = source[key]
|
||||
|
||||
for rule in hass.data[_OVERWRITE_KEY]:
|
||||
for rule in hass.data[overwrite_key]:
|
||||
if CONF_ENTITY_ID in rule:
|
||||
entities = rule[CONF_ENTITY_ID]
|
||||
if domain in entities:
|
||||
@@ -74,7 +101,7 @@ def get_overrides(hass: HomeAssistant, entity_id: str) -> Dict:
|
||||
deep_update(result, clean_entry(domain_result))
|
||||
deep_update(result, clean_entry(glob_result))
|
||||
deep_update(result, clean_entry(exact_result))
|
||||
if _OVERWRITE_CACHE_KEY not in hass.data:
|
||||
hass.data[_OVERWRITE_CACHE_KEY] = {}
|
||||
hass.data[_OVERWRITE_CACHE_KEY][entity_id] = result
|
||||
if cache_key not in hass.data:
|
||||
hass.data[cache_key] = {}
|
||||
hass.data[cache_key][entity_id] = result
|
||||
return result
|
||||
@@ -11,7 +11,7 @@ from homeassistant.const import (
|
||||
ATTR_UNIT_OF_MEASUREMENT, DEVICE_DEFAULT_NAME, STATE_OFF, STATE_ON,
|
||||
STATE_UNAVAILABLE, STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT,
|
||||
ATTR_ENTITY_PICTURE)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, DOMAIN as CORE_DOMAIN
|
||||
from homeassistant.exceptions import NoEntitySpecifiedError
|
||||
from homeassistant.util import ensure_unique_string, slugify
|
||||
from homeassistant.util.async import (
|
||||
@@ -242,7 +242,7 @@ class Entity(object):
|
||||
end - start)
|
||||
|
||||
# Overwrite properties that have been set in the config file.
|
||||
attr.update(get_overrides(self.hass, self.entity_id))
|
||||
attr.update(get_overrides(self.hass, CORE_DOMAIN, self.entity_id))
|
||||
|
||||
# Remove hidden property if false so it won't show up.
|
||||
if not attr.get(ATTR_HIDDEN, True):
|
||||
|
||||
Reference in new issue
Block a user