Skip to content

Commit cb81862

Browse files
Implement #7 - Allow whitelisting and blacklisting of entities and domains
1 parent ee44189 commit cb81862

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ remote_homeassistant:
6565
access_token: !secret access_token
6666
api_password: !secret http_password
6767
entity_prefix: "slave02_"
68+
include:
69+
domains:
70+
- sensor
71+
- switch
72+
- group
73+
entities:
74+
- zwave.controller
75+
- zwave.desk_light
76+
exclude:
77+
entities:
78+
- group.all_switches
79+
6880
subscribe_events:
6981
- zwave.network_ready
7082
- zwave.node_event
@@ -100,6 +112,28 @@ entity_prefix:
100112
description: Prefix for all entities of the remote instance.
101113
required: false
102114
type: string
115+
include:
116+
description: Configures what should be included from the remote instance. Values set by the exclude lists will take precedence.
117+
required: false
118+
default: include everything
119+
type: mapping of
120+
entities:
121+
description: The list of entity ids to be included from the remote instance
122+
type: list
123+
domains:
124+
description: The list of domains to be included from the remote instance
125+
type: list
126+
exclude:
127+
description: Configures what should be excluded from the remote instance
128+
required: false
129+
default: exclude nothing
130+
type: mapping of
131+
entities:
132+
description: The list of entity ids to be excluded from the remote instance
133+
type: list
134+
domains:
135+
description: The list of domains to be excluded from the remote instance
136+
type: list
103137
subscribe_events:
104138
description: Further list of events, which should be forwarded from the remote instance. If you override this, you probably will want to add state_changed!!
105139
required: false

custom_components/remote_homeassistant/__init__.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from homeassistant.helpers.typing import HomeAssistantType, ConfigType
1919
from homeassistant.const import (CONF_HOST, CONF_PORT, EVENT_CALL_SERVICE,
2020
EVENT_HOMEASSISTANT_STOP,
21-
EVENT_STATE_CHANGED, EVENT_SERVICE_REGISTERED)
21+
EVENT_STATE_CHANGED, EVENT_SERVICE_REGISTERED, CONF_EXCLUDE, CONF_ENTITIES,
22+
CONF_DOMAINS, CONF_INCLUDE)
2223
from homeassistant.config import DATA_CUSTOMIZE
2324
from homeassistant.helpers.aiohttp_client import async_get_clientsession
2425
import homeassistant.helpers.config_validation as cv
@@ -46,6 +47,22 @@
4647
vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
4748
vol.Exclusive(CONF_ACCESS_TOKEN, 'auth'): cv.string,
4849
vol.Exclusive(CONF_API_PASSWORD, 'auth'): cv.string,
50+
vol.Optional(CONF_EXCLUDE, default={}): vol.Schema(
51+
{
52+
vol.Optional(CONF_ENTITIES, default=[]): cv.entity_ids,
53+
vol.Optional(CONF_DOMAINS, default=[]): vol.All(
54+
cv.ensure_list, [cv.string]
55+
),
56+
}
57+
),
58+
vol.Optional(CONF_INCLUDE, default={}): vol.Schema(
59+
{
60+
vol.Optional(CONF_ENTITIES, default=[]): cv.entity_ids,
61+
vol.Optional(CONF_DOMAINS, default=[]): vol.All(
62+
cv.ensure_list, [cv.string]
63+
),
64+
}
65+
),
4966
vol.Optional(CONF_SUBSCRIBE_EVENTS,
5067
default=DEFAULT_SUBSCRIBED_EVENTS): cv.ensure_list,
5168
vol.Optional(CONF_ENTITY_PREFIX, default=DEFAULT_ENTITY_PREFIX): cv.string,
@@ -82,6 +99,16 @@ def __init__(self, hass, conf):
8299
self._verify_ssl = conf.get(CONF_VERIFY_SSL)
83100
self._access_token = conf.get(CONF_ACCESS_TOKEN)
84101
self._password = conf.get(CONF_API_PASSWORD)
102+
103+
# see homeassistant/components/influxdb/__init__.py
104+
# for include/exclude logic
105+
include = conf.get(CONF_INCLUDE, {})
106+
exclude = conf.get(CONF_EXCLUDE, {})
107+
self._whitelist_e = set(include.get(CONF_ENTITIES, []))
108+
self._whitelist_d = set(include.get(CONF_DOMAINS, []))
109+
self._blacklist_e = set(exclude.get(CONF_ENTITIES, []))
110+
self._blacklist_d = set(exclude.get(CONF_DOMAINS, []))
111+
85112
self._subscribe_events = conf.get(CONF_SUBSCRIBE_EVENTS)
86113
self._entity_prefix = conf.get(CONF_ENTITY_PREFIX)
87114

@@ -269,8 +296,22 @@ def _remove_prefix(entity_id):
269296

270297
def state_changed(entity_id, state, attr):
271298
"""Publish remote state change on local instance."""
299+
domain, object_id = split_entity_id(entity_id)
300+
301+
if (
302+
entity_id in self._blacklist_e
303+
or domain in self._blacklist_d
304+
):
305+
return
306+
307+
if (
308+
(self._whitelist_e or self._whitelist_d)
309+
and entity_id not in self._whitelist_e
310+
and domain not in self._whitelist_d
311+
):
312+
return
313+
272314
if self._entity_prefix:
273-
domain, object_id = split_entity_id(entity_id)
274315
object_id = self._entity_prefix + object_id
275316
entity_id = domain + '.' + object_id
276317

0 commit comments

Comments
 (0)