Add minimum/maximum to counter (#22608)

* Added minimum/maximum to counter

* Added min/max testcases

* remove duplicate

* cosmetic changes

* removed blank lines at eof

* added newline at eof

* type cv -> vol

* more fixes

* - fixed min/max warnings
- fixed failing tests

* Added linewrap

* - Added cast to int
- Fixed double quotes

* - removed None check in __init__
- fixed failing test

* copy paste fix

* copy paste fix

* Added possibility to change counter properties trough service call

* fixed copy paste errors

* Added '.' to comment

* rephrased docstring

* Fix tests after rebase

* Clean up per previous code review comments

* Replace setup service with configure

* Update services description

* Update tests to use configure instead of setup
This commit is contained in:
Rohan Kapoor authored and GitHub committed 2019-04-18 19:02:01 +09:00
1 parent 6e4083d7f4
commit f588fef3b4
3 files changed
+238 -19

No files matched your search

+159 -6
View File
@@ -3,13 +3,13 @@
import asyncio
import logging
from homeassistant.core import CoreState, State, Context
from homeassistant.components.counter import (CONF_ICON, CONF_INITIAL,
CONF_NAME, CONF_RESTORE,
CONF_STEP, DOMAIN)
from homeassistant.const import (ATTR_FRIENDLY_NAME, ATTR_ICON)
from homeassistant.core import Context, CoreState, State
from homeassistant.setup import async_setup_component
from homeassistant.components.counter import (
DOMAIN, CONF_INITIAL, CONF_RESTORE, CONF_STEP, CONF_NAME, CONF_ICON)
from homeassistant.const import (ATTR_ICON, ATTR_FRIENDLY_NAME)
from tests.common import mock_restore_cache
from tests.common import (mock_restore_cache)
from tests.components.counter.common import (
async_decrement, async_increment, async_reset)
@@ -243,3 +243,156 @@ async def test_counter_context(hass, hass_admin_user):
assert state2 is not None
assert state.state != state2.state
assert state2.context.user_id == hass_admin_user.id
async def test_counter_min(hass, hass_admin_user):
"""Test that min works."""
assert await async_setup_component(hass, 'counter', {
'counter': {
'test': {
'minimum': '0',
'initial': '0'
}
}
})
state = hass.states.get('counter.test')
assert state is not None
assert state.state == '0'
await hass.services.async_call('counter', 'decrement', {
'entity_id': state.entity_id,
}, True, Context(user_id=hass_admin_user.id))
state2 = hass.states.get('counter.test')
assert state2 is not None
assert state2.state == '0'
await hass.services.async_call('counter', 'increment', {
'entity_id': state.entity_id,
}, True, Context(user_id=hass_admin_user.id))
state2 = hass.states.get('counter.test')
assert state2 is not None
assert state2.state == '1'
async def test_counter_max(hass, hass_admin_user):
"""Test that max works."""
assert await async_setup_component(hass, 'counter', {
'counter': {
'test': {
'maximum': '0',
'initial': '0'
}
}
})
state = hass.states.get('counter.test')
assert state is not None
assert state.state == '0'
await hass.services.async_call('counter', 'increment', {
'entity_id': state.entity_id,
}, True, Context(user_id=hass_admin_user.id))
state2 = hass.states.get('counter.test')
assert state2 is not None
assert state2.state == '0'
await hass.services.async_call('counter', 'decrement', {
'entity_id': state.entity_id,
}, True, Context(user_id=hass_admin_user.id))
state2 = hass.states.get('counter.test')
assert state2 is not None
assert state2.state == '-1'
async def test_configure(hass, hass_admin_user):
"""Test that setting values through configure works."""
assert await async_setup_component(hass, 'counter', {
'counter': {
'test': {
'maximum': '10',
'initial': '10'
}
}
})
state = hass.states.get('counter.test')
assert state is not None
assert state.state == '10'
assert 10 == state.attributes.get('maximum')
# update max
await hass.services.async_call('counter', 'configure', {
'entity_id': state.entity_id,
'maximum': 0,
}, True, Context(user_id=hass_admin_user.id))
state = hass.states.get('counter.test')
assert state is not None
assert state.state == '0'
assert 0 == state.attributes.get('maximum')
# disable max
await hass.services.async_call('counter', 'configure', {
'entity_id': state.entity_id,
'maximum': None,
}, True, Context(user_id=hass_admin_user.id))
state = hass.states.get('counter.test')
assert state is not None
assert state.state == '0'
assert state.attributes.get('maximum') is None
# update min
assert state.attributes.get('minimum') is None
await hass.services.async_call('counter', 'configure', {
'entity_id': state.entity_id,
'minimum': 5,
}, True, Context(user_id=hass_admin_user.id))
state = hass.states.get('counter.test')
assert state is not None
assert state.state == '5'
assert 5 == state.attributes.get('minimum')
# disable min
await hass.services.async_call('counter', 'configure', {
'entity_id': state.entity_id,
'minimum': None,
}, True, Context(user_id=hass_admin_user.id))
state = hass.states.get('counter.test')
assert state is not None
assert state.state == '5'
assert state.attributes.get('minimum') is None
# update step
assert 1 == state.attributes.get('step')
await hass.services.async_call('counter', 'configure', {
'entity_id': state.entity_id,
'step': 3,
}, True, Context(user_id=hass_admin_user.id))
state = hass.states.get('counter.test')
assert state is not None
assert state.state == '5'
assert 3 == state.attributes.get('step')
# update all
await hass.services.async_call('counter', 'configure', {
'entity_id': state.entity_id,
'step': 5,
'minimum': 0,
'maximum': 9,
}, True, Context(user_id=hass_admin_user.id))
state = hass.states.get('counter.test')
assert state is not None
assert state.state == '5'
assert 5 == state.attributes.get('step')
assert 0 == state.attributes.get('minimum')
assert 9 == state.attributes.get('maximum')