Skip to content

Commit 6a4aeaa

Browse files
committed
Working autodiscovery
1 parent 6807961 commit 6a4aeaa

File tree

6 files changed

+623
-213
lines changed

6 files changed

+623
-213
lines changed

README.md

Lines changed: 111 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,75 @@ The integration supports two configuration methods:
7878

7979
#### Method 1: Auto-Discovery (Recommended)
8080

81-
Use the built-in auto-discovery to find and configure devices automatically:
81+
Use the built-in auto-discovery to find and configure devices automatically.
82+
83+
**Important:** Auto-discovery is activated via Home Assistant services, NOT via YAML configuration.
84+
85+
**Step 1: Prepare Configuration File**
86+
87+
Create an empty or basic `/config/myhome.yaml` file:
88+
89+
```yaml
90+
# MyHOME Configuration
91+
# Add your gateway configurations here
92+
# (discovered devices will be automatically added)
93+
```
94+
95+
**Step 2: Start Discovery**
96+
97+
Use Home Assistant **Developer Tools → Services** to call:
8298

8399
```yaml
84-
# Start discovery via Home Assistant service
85100
service: myhome.start_discovery
86101
data:
87-
gateway: "00:03:50:XX:XX:XX" # Optional: specify gateway MAC
102+
gateway: "00:03:50:XX:XX:XX" # Your gateway MAC address
103+
```
104+
105+
**OR** create an automation to start discovery:
106+
107+
```yaml
108+
automation:
109+
- alias: "Start MyHOME Discovery"
110+
trigger:
111+
platform: homeassistant
112+
event: start
113+
action:
114+
service: myhome.start_discovery
115+
data:
116+
gateway: "00:03:50:XX:XX:XX"
88117
```
89118
90119
**Discovery Process:**
91-
1. Call the `myhome.start_discovery` service
92-
2. The integration scans for devices automatically
93-
3. Found devices appear in the **Devices & Services** page
94-
4. Click **"Configure"** on discovered devices to add them
95-
5. Adjust suggested configurations as needed
120+
1. Call the `myhome.start_discovery` service with your gateway MAC
121+
2. The integration sends discovery commands to scan for devices (`*#1*0##`, `*#2*0##`, etc.)
122+
3. Device responses come back as events and are processed by the discovery service
123+
4. Discovered devices are automatically added to `/config/myhome.yaml`
124+
5. The integration reloads and new entities appear in Home Assistant
125+
6. Check logs for discovery progress and any issues
126+
127+
**Common Discovery Issues:**
128+
129+
❌ **Wrong**: Putting service calls in YAML config:
130+
```yaml
131+
# DON'T DO THIS - This goes in myhome.yaml config file
132+
service: myhome.start_discovery
133+
data:
134+
mac: '00:03:50:XX:XX:XX'
135+
```
136+
137+
✅ **Correct**: Call service through Home Assistant:
138+
```yaml
139+
# Use Developer Tools → Services or automation
140+
service: myhome.start_discovery
141+
data:
142+
gateway: '00:03:50:XX:XX:XX'
143+
```
144+
145+
**Discovery Warning Messages:**
146+
- `"Could not send message *#18*0##"` - Energy management subsystem not available
147+
- `"Could not send message *#9*0##"` - Auxiliary subsystem not available
148+
- These warnings are **normal** if your gateway doesn't support these subsystems
149+
- Discovery will still find devices from supported subsystems (lighting, automation, etc.)
96150

97151
#### Method 2: Manual Configuration (YAML)
98152

@@ -323,10 +377,55 @@ automation:
323377

324378
#### Device Discovery Issues
325379

326-
1. **Start discovery manually**: Use the `myhome.start_discovery` service
327-
2. **Check OpenWebNet addresses**: Verify device WHERE addresses are correct
328-
3. **Monitor gateway communication**: Enable debug logging
329-
4. **Restart integration**: Reload the integration from Devices & Services
380+
**"Discovery not active" in logs:**
381+
- Ensure you're calling the service correctly: `service: myhome.start_discovery` with `gateway: "MAC_ADDRESS"`
382+
- Don't put service calls in the YAML config file - use Developer Tools → Services
383+
- Check that the gateway MAC address is correct
384+
- Verify the service call shows `discovery_active: True` in debug logs
385+
386+
**No devices found during discovery:**
387+
1. **Enable debug logging** to see discovery messages:
388+
```yaml
389+
logger:
390+
logs:
391+
custom_components.myhome.discovery: debug
392+
custom_components.myhome.gateway: debug
393+
custom_components.myhome.config_flow_discovery: debug
394+
```
395+
2. **Check discovery status** - Look for logs like:
396+
- `"Starting MyHOME device discovery on gateway..."`
397+
- `"Sending discovery command 1/6: *#1*0##"`
398+
- `"Discovery message received: *1*8*11##"`
399+
- `"Discovered new device: MyHOME Bus Dimmer 11 at WHERE=11"`
400+
- `"Starting device configuration suggestion for MyHOME Bus Dimmer 11"`
401+
- `"Starting config file write process for device MyHOME Bus Dimmer 11"`
402+
- `"Successfully added device MyHOME Bus Dimmer 11 to configuration file"`
403+
3. **Verify device responses** - Look for incoming messages after discovery commands
404+
4. **Check gateway communication** - Ensure devices are responding to status requests
405+
5. **Manual device test** - Try controlling devices through other MyHOME apps first
406+
407+
**Incorrect device type detection:**
408+
- **Dimmer vs Switch**: Discovery determines device type based on status responses
409+
- Devices reporting dimming levels (WHAT=2-10, excluding 8) are detected as dimmers
410+
- Devices reporting only ON/OFF states (WHAT=0,1,8) are detected as switches
411+
- If a dimmer is incorrectly detected as a switch, manually edit the config and set `dimmable: true`
412+
- **Special states**: WHAT=8 often indicates "temporized ON" or other special states, not dimming capability
413+
414+
**Devices discovered but not added:**
415+
1. **Check `/config/myhome.yaml`** - devices should be automatically added
416+
2. **Verify file permissions** - ensure Home Assistant can write to the config file
417+
3. **Monitor config file writing** - Look for debug logs like:
418+
- `"Starting config file write process for device..."`
419+
- `"Reading existing config file..."`
420+
- `"Writing updated config to file..."`
421+
- `"Config file write completed successfully"`
422+
- `"Config file size after write: XXX bytes"`
423+
4. **Check for config write errors** - Look for error logs like:
424+
- `"Error writing to config file"`
425+
- `"Failed to add device to config file"`
426+
- `"Error in config file write process"`
427+
5. **Monitor integration reload** - check logs for config reload errors
428+
6. **Restart integration** manually if auto-reload fails
330429

331430
#### Configuration Issues
332431

custom_components/myhome/__init__.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
6464

6565
try:
6666
async with aiofiles.open(_config_file_path, mode="r") as yaml_file:
67-
_validated_config = config_schema(yaml.safe_load(await yaml_file.read()))
67+
yaml_content = await yaml_file.read()
68+
parsed_yaml = yaml.safe_load(yaml_content)
69+
# Handle empty or invalid YAML content
70+
if parsed_yaml is None or not isinstance(parsed_yaml, dict):
71+
LOGGER.info(f"Configuration file '{_config_file_path}' is empty or invalid, using empty configuration")
72+
_validated_config = {}
73+
else:
74+
# Filter out 'service' key if present (not part of device config)
75+
if 'service' in parsed_yaml:
76+
LOGGER.info("Filtering out 'service' key from configuration - not supported in device config")
77+
parsed_yaml = {k: v for k, v in parsed_yaml.items() if k != 'service'}
78+
_validated_config = config_schema(parsed_yaml)
6879
except FileNotFoundError:
6980
LOGGER.info(f"Configuration file '{_config_file_path}' not found, creating empty configuration file")
7081
try:
@@ -77,10 +88,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
7788
LOGGER.error(f"Failed to create configuration file '{_config_file_path}': {e}")
7889
return False
7990

80-
if entry.data[CONF_MAC] in _validated_config:
81-
hass.data[DOMAIN][entry.data[CONF_MAC]] = _validated_config[
82-
entry.data[CONF_MAC]
83-
]
91+
# Check for config under "gateway" key first, then MAC address for backward compatibility
92+
gateway_config = None
93+
if "gateway" in _validated_config:
94+
gateway_config = _validated_config["gateway"]
95+
elif entry.data[CONF_MAC] in _validated_config:
96+
gateway_config = _validated_config[entry.data[CONF_MAC]]
97+
98+
if gateway_config:
99+
hass.data[DOMAIN][entry.data[CONF_MAC]] = gateway_config
84100
else:
85101
# Initialize empty configuration for this gateway - will be populated via config flow
86102
LOGGER.info(f"Gateway {entry.data[CONF_MAC]} not found in configuration file, initializing with empty configuration")
@@ -156,13 +172,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
156172

157173
hass.data[DOMAIN][entry.data[CONF_MAC]][CONF_ENTITY].listening_worker = (
158174
entry.async_create_background_task(
175+
hass,
159176
hass.data[DOMAIN][entry.data[CONF_MAC]][CONF_ENTITY].listening_loop(),
160177
name="myhome_listening_worker",
161178
)
162179
)
163180
for i in range(_command_worker_count):
164181
hass.data[DOMAIN][entry.data[CONF_MAC]][CONF_ENTITY].sending_workers.append(
165182
entry.async_create_background_task(
183+
hass,
166184
hass.data[DOMAIN][entry.data[CONF_MAC]][CONF_ENTITY].sending_loop(i),
167185
name=f"myhome_sending_worker_{i}",
168186
)

0 commit comments

Comments
 (0)