Created simple counter sensor#6880
Conversation
| from homeassistant.components.sensor import DOMAIN, PLATFORM_SCHEMA | ||
| from homeassistant.const import CONF_NAME | ||
| from homeassistant.helpers.entity import Entity | ||
| from homeassistant.util import Throttle |
There was a problem hiding this comment.
'homeassistant.util.Throttle' imported but unused
| https://home-assistant.io/components/sensor.counter/ | ||
| """ | ||
| import logging | ||
| from datetime import timedelta |
There was a problem hiding this comment.
'datetime.timedelta' imported but unused
balloob
left a comment
There was a problem hiding this comment.
Off to a good start.
Some changes needed and also will require tests to be written to verify that all the logic works.
| data.reset() | ||
| sensor.update() | ||
|
|
||
| hass.services.register(DOMAIN, 'increment_counter', increment_counter) |
There was a problem hiding this comment.
Platforms that register services need to prefix the services with the name of the platform.
There was a problem hiding this comment.
With prefix, do you mean the DOMAIN part that should be changed, or should the service call itself by changed (to counter_increment then I guess)?
| def increment_counter(call=None): | ||
| """Increment counter and update sensor.""" | ||
| data.increment() | ||
| sensor.update() |
There was a problem hiding this comment.
This is not going to work with multiple counters.
There was a problem hiding this comment.
I realised this, but I couldn't see a direct way on how to do it, I guess it might need to be done with entity_ids feeding into the call? I looked through some other files, but couldn't directly find a good example. I'll try to spend some time on figuring this out
There was a problem hiding this comment.
You can store a list of entities in hass.data. That is a dictionary that anyone can use.
KEY_COUNTERS = 'counters'
hass.data.setdefault(KEY_COUNTERS, [])
entity = …
hass.data[KEY_COUNTERS].append(entity)
add_devices([entity])|
|
||
| def increment(self): | ||
| """Increment the counter.""" | ||
| _LOGGER.info('Incrementing counter') |
There was a problem hiding this comment.
This should not be log level info
|
|
||
| def decrement(self): | ||
| """Decrement the counter.""" | ||
| _LOGGER.info('Decrementing counter') |
There was a problem hiding this comment.
This should not be log level info
|
|
||
| def reset(self): | ||
| """Reset the counter.""" | ||
| _LOGGER.info('Resetting counter') |
There was a problem hiding this comment.
This should not be log level info
|
This PR seems to have gone stale. Closing it. You can reopen it when you're ready to finish it. |
Description:
I've created a simple counter sensor. It has 3 services which can be invoked, an increment, decrement and reset one.
The initial value and steps are also configurable.
I've modeled the sensor after fastdotcom, using a seperate class to hold and act on the data.
NOTE: Tests and docs still left to do
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.github.io#<home-assistant.github.io PR number goes here>
Still TODO
Example entry for
configuration.yaml(if applicable):Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code does not interact with devices:
toxrun successfully. Your PR cannot be merged unless tests pass