Skip to content

Created simple counter sensor#6880

Closed
Tommatheussen wants to merge 3 commits into
home-assistant:devfrom
Tommatheussen:patch-1
Closed

Created simple counter sensor#6880
Tommatheussen wants to merge 3 commits into
home-assistant:devfrom
Tommatheussen:patch-1

Conversation

@Tommatheussen
Copy link
Copy Markdown
Contributor

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):

sensor:
  - platform: counter
    initial: 10 (optional)
    step: 3 (optional)

Checklist:

If user exposed functionality or configuration variables are added/changed:

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'homeassistant.util.Throttle' imported but unused

https://home-assistant.io/components/sensor.counter/
"""
import logging
from datetime import timedelta
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'datetime.timedelta' imported but unused

Copy link
Copy Markdown
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Platforms that register services need to prefix the services with the name of the platform.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

counter_increment.

def increment_counter(call=None):
"""Increment counter and update sensor."""
data.increment()
sensor.update()
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Member

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.

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')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be log level info


def decrement(self):
"""Decrement the counter."""
_LOGGER.info('Decrementing counter')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be log level info


def reset(self):
"""Reset the counter."""
_LOGGER.info('Resetting counter')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be log level info

@balloob
Copy link
Copy Markdown
Member

balloob commented Apr 19, 2017

This PR seems to have gone stale. Closing it. You can reopen it when you're ready to finish it.

@balloob balloob closed this Apr 19, 2017
@home-assistant home-assistant locked and limited conversation to collaborators Aug 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants