-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Created simple counter sensor #6880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """ | ||
| Support for keeping a simple counter. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/sensor.counter/ | ||
| """ | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.sensor import DOMAIN, PLATFORM_SCHEMA | ||
| from homeassistant.const import CONF_NAME | ||
| from homeassistant.helpers.entity import Entity | ||
| import homeassistant.helpers.config_validation as cv | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| CONF_INITIAL = 'initial' | ||
| CONF_STEP = 'step' | ||
|
|
||
| DEFAULT_NAME = 'Counter' | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, | ||
| vol.Optional(CONF_INITIAL, default=0): vol.Coerce(int), | ||
| vol.Optional(CONF_STEP, default=1): vol.Coerce(int) | ||
| }) | ||
|
|
||
|
|
||
| def setup_platform(hass, config, add_devices, discovery_info=None): | ||
| """Setup the sensor platform.""" | ||
| data = CounterData(hass, config) | ||
| sensor = CounterSensor(data, config.get(CONF_NAME)) | ||
| add_devices([sensor]) | ||
|
|
||
| def increment_counter(call=None): | ||
| """Increment counter and update sensor.""" | ||
| data.increment() | ||
| sensor.update() | ||
|
|
||
| def decrement_counter(call=None): | ||
| """Decrement counter and update sensor.""" | ||
| data.decrement() | ||
| sensor.update() | ||
|
|
||
| def reset_counter(call=None): | ||
| """Reset counter and update sensor.""" | ||
| data.reset() | ||
| sensor.update() | ||
|
|
||
| hass.services.register(DOMAIN, 'counter_increment', increment_counter) | ||
| hass.services.register(DOMAIN, 'counter_reset', reset_counter) | ||
| hass.services.register(DOMAIN, 'counter_decrement', decrement_counter) | ||
|
|
||
|
|
||
| class CounterSensor(Entity): | ||
| """Representation of a counter sensor.""" | ||
|
|
||
| def __init__(self, counter_data, name): | ||
| """Initialize the sensor.""" | ||
| self.counter_client = counter_data | ||
| self._state = self.counter_client.count | ||
| self._name = name | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return the state of the sensor.""" | ||
| return self._state | ||
|
|
||
| def update(self): | ||
| """Get the latest value from the data client.""" | ||
| self._state = self.counter_client.count | ||
|
|
||
|
|
||
| class CounterData(object): | ||
| """Keep all counter data and actions.""" | ||
|
|
||
| def __init__(self, hass, config): | ||
| """Initialize the data client.""" | ||
| self._initial = config[CONF_INITIAL] | ||
| self._step = config[CONF_STEP] | ||
| self.count = self._initial | ||
|
|
||
| def increment(self): | ||
| """Increment the counter.""" | ||
| _LOGGER.debug('Incrementing counter') | ||
| self.count += self._step | ||
|
|
||
| def decrement(self): | ||
| """Decrement the counter.""" | ||
| _LOGGER.debug('Decrementing counter') | ||
| self.count -= self._step | ||
|
|
||
| def reset(self): | ||
| """Reset the counter.""" | ||
| _LOGGER.debug('Resetting counter') | ||
| self.count = self._initial | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not going to work with multiple counters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can store a list of entities in
hass.data. That is a dictionary that anyone can use.