Skip to content

Commit 59dfab3

Browse files
committed
Allow user to specify entity IDs directly
1 parent 0afc86e commit 59dfab3

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Here's how it works:
1919
3. If you set `save_file` to false, go to the Home Assistant logs to copy the generated YAML for the scene.
2020
4. If you set `save_file` to true, go to your configuration folder and open `generated_scene.yaml`.
2121

22+
This script allows you to specify either domains or entities directly. If both domains and entities are defined, the script will prioritize entities over domains.
23+
2224
### EXAMPLE SERVICE DATA
2325

2426
![example service call](https://github.com/sunnythaper/python_scripts/raw/master/readme-assets/service_call.png)
@@ -28,6 +30,9 @@ domains:
2830
- light
2931
- switch
3032
- fan
33+
entities:
34+
- light.living_room
35+
- switch.kitchen
3136
attributes:
3237
- brightness
3338
- color_temp
@@ -36,7 +41,7 @@ attributes:
3641
save_file: true
3742
```
3843
39-
In this example, we're generating a scene for all light, switch, and fan entities, and including brightness, color temperature, and color attributes.
44+
In this example, we're generating a scene for all light, switch, and fan entities defined in the domains, as well as specifically for `light.living_room` and `switch.kitchen` entities. It includes brightness, color temperature, and color attributes. If both domains and entities are defined, the script will process only the entities and ignore the domains.
4045

4146
### FILE SAVING
4247

readme-assets/service_call.png

1.81 KB
Loading

scene_generator.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,40 @@ def format_entity_status(entity, status, attributes):
66
for attribute in attributes if status.attributes.get(attribute)]
77
return f"{entity}:\n state: {status.state}\n" + "".join(attribute_texts) + "\n"
88

9-
def process_domain(domain, attributes):
10-
text_lines = [format_header(domain)]
11-
entities = hass.states.entity_ids(domain)
9+
def process_entities(entities, attributes):
10+
text_lines = []
1211
for entity_id in entities:
1312
entity_status = hass.states.get(entity_id)
1413
text_lines.append(format_entity_status(entity_id, entity_status, attributes))
1514
return "".join(text_lines)
1615

16+
def process_domain(domain, attributes):
17+
text_lines = [format_header(domain)]
18+
entities = hass.states.entity_ids(domain)
19+
text_lines.append(process_entities(entities, attributes))
20+
return "".join(text_lines)
21+
1722
def process_domains(domains, attributes):
23+
if not domains:
24+
return ""
1825
return "".join(process_domain(domain, attributes) for domain in domains)
1926

27+
def process_input(domains, entities, attributes):
28+
if entities:
29+
return process_entities(entities, attributes)
30+
else:
31+
return process_domains(domains, attributes)
32+
2033
def export_results(text, save_file):
2134
if save_file:
2235
hass.services.call("notify", "scene_generator", {"message": "{}".format(text)})
2336
else:
2437
logger.info(text)
2538

2639
domains = data.get('domains', ['light','switch','fan'])
40+
entities = data.get('entities')
2741
attributes = data.get('attributes', ['brightness','color_temp','xy_color','rgb_color'])
2842
save_file = data.get('save_file')
2943

30-
text = process_domains(domains, attributes)
44+
text = process_input(domains, entities, attributes)
3145
export_results(text, save_file)

services.yaml

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
scene_generator:
22
name: Scene Generator
3-
description: Generate a scene based on the state of specific entities in specified domains.
3+
description: Generate a scene based on the state of specific entities in specified domains or directly defined entities.
44
fields:
55
domains:
6-
description: The domains to be processed for generating the scene.
6+
description: The domains to be processed for generating the scene. Ignored if entities are defined.
77
example:
88
- light
99
- switch
1010
- fan
11+
entities:
12+
description: Specific entities to be processed for generating the scene. Takes precedence over domains if both are defined.
13+
example:
14+
- light.living_room
15+
- switch.kitchen
1116
attributes:
1217
description: The attributes of the entities to be processed.
1318
example:

0 commit comments

Comments
 (0)