Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions homeassistant/components/sensor/counter.py
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()
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 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