esphome / esphome-core

🚨 No longer used 🚨 - The C++ framework behind ESPHome
https://esphome.io/
GNU General Public License v3.0
545 stars 113 forks source link

Custom Sensor Component name does not get used #445

Closed J4nsen closed 5 years ago

J4nsen commented 5 years ago

Operating environment/Installation (Hass.io/Docker/pip/etc.):

pip installation on arch linux (v1.10.1)

ESP (ESP32/ESP8266/Board/Sonoff):

Wemos Lolin32

Affected component:

https://esphomelib.com/esphomeyaml/components/sensor/custom.html

Description of problem: Hi, I want to write a custom I2C Component to read out a MAG3110 sensor, which should count the rotations of a magnet inside my gas meter. I started with the custom sensor example, which somewhat works. I'm not able to give the sensor a name. The test value "42" gets published to 'iot_esp32_gaszaehler/sensor//state', but it should be published to 'iot_esp32_gaszaehler/sensor/my_custom_sensor/state' if I understand the documentation correctly. What am I doing wrong?

Problem-relevant YAML-configuration entries:

esphomeyaml:
  name: iot_esp32_gaszaehler
  platform: ESP32
  board: lolin32
  includes:
          - include/my_custom_sensor.h

wifi: !include common/wifi.yaml
mqtt: !include common/mqtt.yaml

sensor:
- platform: custom
  lambda: |-
    auto my_sensor = new MyCustomSensor();
    App.register_component(my_sensor);
    return {my_sensor};

  sensors:
    name: "My Custom Sensor"

# Enable logging
logger:

ota: !include common/ota.yaml

Traceback (if applicable):

Additional information:

INFO Starting log output from /dev/ttyUSB1 with baud rate 115200
[18:46:25][I][logger:071]: Log initialized
[18:46:25][C][ota:461]: There have been 0 suspected unsuccessful boot attempts.
[18:46:25][I][application:053]: Running through setup()...
[18:46:25][C][wifi:029]: Setting up WiFi...
[18:46:25][D][wifi:260]: Starting scan...
[18:46:27][D][wifi:275]: Found networks:
[18:46:27][I][wifi:308]: - 'xyz-iot' () β–‚β–„β–†β–ˆ
[18:46:27][D][wifi:309]:     Channel: 11
[18:46:27][D][wifi:310]:     RSSI: -68 dB
[18:46:27][I][wifi:308]: - 'xyz-iot' () β–‚β–„β–†β–ˆ
[18:46:27][D][wifi:309]:     Channel: 1
[18:46:27][D][wifi:310]:     RSSI: -91 dB
[18:46:27][D][wifi:312]: - 'xyz' () β–‚β–„β–†β–ˆ
[18:46:27][D][wifi:312]: - 'xyz-gast' () β–‚β–„β–†β–ˆ
[18:46:27][D][wifi:312]: - 'xyz-gast' () β–‚β–„β–†β–ˆ
[18:46:27][D][wifi:312]: - 'xyz' () β–‚β–„β–†β–ˆ
[18:46:27][I][wifi:179]: WiFi Connecting to 'xyz-iot'...
[18:46:29][I][wifi:349]: WiFi connected!
[18:46:29][C][wifi:240]:   SSID: 'xyz-iot'
[18:46:29][C][wifi:241]:   IP Address: 192.168.222.146
[18:46:29][C][wifi:243]:   BSSID: 
[18:46:29][C][wifi:245]:   Hostname: 'iot_esp32_gaszaehler'
[18:46:29][C][wifi:250]:   Signal strength: -64 dB β–‚β–„β–†β–ˆ
[18:46:29][C][wifi:251]:   Channel: 11
[18:46:29][C][wifi:252]:   Subnet: 255.255.255.0
[18:46:29][C][wifi:253]:   Gateway: 192.168.222.2
[18:46:29][C][wifi:254]:   DNS1: 192.168.222.2
[18:46:29][C][wifi:255]:   DNS2: 0.0.0.0
[18:46:29][C][ota:129]: Over-The-Air Updates:
[18:46:29][C][ota:130]:   Address: 192.168.222.146:3232
[18:46:29][C][ota:132]:   Using Password.
[18:46:29][C][mqtt.client:024]: Setting up MQTT...
[18:46:29][I][mqtt.client:170]: Connecting to MQTT...
[18:46:29][I][mqtt.client:209]: MQTT Connected!
[18:46:29][I][application:089]: setup() finished successfully!
[18:46:29][I][application:097]: You're running esphomelib v1.10.1 compiled on Jan 25 2019, 18:37:46
[18:46:29][C][wifi:341]: WiFi:
[18:46:29][C][wifi:240]:   SSID: 'xyz-iot'
[18:46:29][C][wifi:241]:   IP Address: 192.168.222.146
[18:46:29][C][wifi:243]:   BSSID: 
[18:46:29][C][wifi:245]:   Hostname: 'iot_esp32_gaszaehler'
[18:46:29][C][wifi:250]:   Signal strength: -64 dB β–‚β–„β–†β–ˆ
[18:46:29][C][wifi:251]:   Channel: 11
[18:46:29][C][wifi:252]:   Subnet: 255.255.255.0
[18:46:29][C][wifi:253]:   Gateway: 192.168.222.2
[18:46:29][C][wifi:254]:   DNS1: 192.168.222.2
[18:46:29][C][wifi:255]:   DNS2: 0.0.0.0
[18:46:29][C][logger:099]: Logger:
[18:46:29][C][logger:100]:   Level: DEBUG
[18:46:29][C][logger:101]:   Log Baud Rate: 115200
[18:46:29][C][ota:129]: Over-The-Air Updates:
[18:46:29][C][ota:130]:   Address: 192.168.222.146:3232
[18:46:29][C][ota:132]:   Using Password.
[18:46:29][C][mqtt.client:058]: MQTT:
[18:46:29][C][mqtt.client:060]:   Server Address: 192.168.222.1:1883 (192.168.222.1)
[18:46:29][C][mqtt.client:061]:   Username: ''
[18:46:30][C][mqtt.client:062]:   Client ID: 'iot_esp32_gaszaehler-123'
[18:46:30][C][mqtt.client:064]:   Discovery prefix: 'homeassistant'
[18:46:30][C][mqtt.client:065]:   Discovery retain: YES
[18:46:30][C][mqtt.client:067]:   Topic Prefix: 'iot_esp32_gaszaehler'
[18:46:30][C][mqtt.client:069]:   Log Topic: 'iot_esp32_gaszaehler/debug'
[18:46:30][C][mqtt.client:072]:   Availability: 'iot_esp32_gaszaehler/status'
[18:46:30][C][sensor.mqtt:029]: MQTT Sensor '':
[18:46:30][C][sensor.mqtt:033]:   State Topic: 'iot_esp32_gaszaehler/sensor//state'
[18:46:30][I][application:114]: Running through first loop()
[18:46:30][I][application:141]: First loop finished successfully!
stelb commented 5 years ago

now my sensor names lead to an exception instead of just beeing ignored. removing the names, fixes the exception.

Traceback (most recent call last):
  File "/Users/slebreton/Library/Python/2.7/bin/esphomeyaml", line 11, in <module>
    sys.exit(main())
  File "/Users/slebreton/Library/Python/2.7/lib/python/site-packages/esphomeyaml/__main__.py", line 508, in main
    return run_esphomeyaml(sys.argv)
  File "/Users/slebreton/Library/Python/2.7/lib/python/site-packages/esphomeyaml/__main__.py", line 498, in run_esphomeyaml
    return POST_CONFIG_ACTIONS[args.command](args, config)
  File "/Users/slebreton/Library/Python/2.7/lib/python/site-packages/esphomeyaml/__main__.py", line 304, in command_run
    exit_code = write_cpp(config)
  File "/Users/slebreton/Library/Python/2.7/lib/python/site-packages/esphomeyaml/__main__.py", line 141, in write_cpp
    CORE.flush_tasks()
  File "/Users/slebreton/Library/Python/2.7/lib/python/site-packages/esphomeyaml/core.py", line 395, in flush_tasks
    next(task)
  File "/Users/slebreton/Library/Python/2.7/lib/python/site-packages/esphomeyaml/core.py", line 375, in func_
    for _ in func(*args):
  File "/Users/slebreton/Library/Python/2.7/lib/python/site-packages/esphomeyaml/components/sensor/custom.py", line 29, in to_code
    add(var.set_name(config[CONF_NAME]))

my custom sensor config:

sensor:
- platform: custom
  lambda: |-
    auto my_sensor = new MPU9250Sensor();
    App.register_component(my_sensor);
    return {
      my_sensor->accelX_sensor, my_sensor->accelY_sensor, my_sensor->accelZ_sensor,
      my_sensor->magX_sensor, my_sensor->magY_sensor, my_sensor->magZ_sensor
    };

  sensors:
  - name: "accel X"
  - name: "accel Y"
  - name: "accel Z"
  - name: "mag X"
  - name: "mag Y"
  - name: "mag Z"