Lyr3x / Roode

A reliable smart home people counter based on VL53L1X and ESPHome
The Unlicense
149 stars 41 forks source link

Refactor people counter to a number component #83

Closed CarsonF closed 2 years ago

CarsonF commented 2 years ago

This replaces the sensor for people counts with a number. Numbers are controllable which makes it much easier to change the state in HA UI & automation without having to create lambda API services.

This number is configured for people counting so min=0, step=1, max is configurable.

It is an instance of a new PersistedNumber object I created, that saves/restores the value using a native esphome API. This removes our explicit EEPROM dependency.

This flash write is debounced and can be user configured, defaults to once a minute. https://esphome.io/components/esphome.html#adjusting-flash-writes In testing it seems pending writes are still saved on graceful reboot. Note that ESP8266 have this disabled by default, so...

esp8266:
  restore_from_flash: true

Since this count was the only value that was persisted/"restored", I removed the restore_values config option in favor of restore_value on the number component.

~I couldn't figure out how to reference the roode platform outside of its platform setup, so for now it's just there. I suspect I just missed something and it can be moved to an independent number entry with the roode platform.~

number:
  - platform: roode
    people_counter:
      name: $friendly_name people counter
      max_value: 5 # default 10
Screen Shot 2022-01-01 at 6 14 54 PM
CarsonF commented 2 years ago

Then you can remove the whole people counter from __init__.py

Yeah I tried all of that yesterday with a number file—I couldn't access roode config. I think I was missing this line to make it work:

 cv.GenerateID(CONF_ROODE_ID): cv.use_id(Roode),

I also wanted to move persisted number python code to a separate persisted_number.py but I think esphome tried to load it because of MULTI_CONF so I gave up and merged it back into the file.

Lyr3x commented 2 years ago

In ESPHome sense it is a number so just put it to the number.py like above and we are good to go 👍