Counter configure with value (#28066)
parent
4935ef5233
commit
92508af253
|
@ -16,6 +16,7 @@ ATTR_INITIAL = "initial"
|
|||
ATTR_STEP = "step"
|
||||
ATTR_MINIMUM = "minimum"
|
||||
ATTR_MAXIMUM = "maximum"
|
||||
VALUE = "value"
|
||||
|
||||
CONF_INITIAL = "initial"
|
||||
CONF_RESTORE = "restore"
|
||||
|
@ -37,6 +38,8 @@ SERVICE_SCHEMA_CONFIGURE = ENTITY_SERVICE_SCHEMA.extend(
|
|||
vol.Optional(ATTR_MINIMUM): vol.Any(None, vol.Coerce(int)),
|
||||
vol.Optional(ATTR_MAXIMUM): vol.Any(None, vol.Coerce(int)),
|
||||
vol.Optional(ATTR_STEP): cv.positive_int,
|
||||
vol.Optional(ATTR_INITIAL): cv.positive_int,
|
||||
vol.Optional(VALUE): cv.positive_int,
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -171,6 +174,10 @@ class Counter(RestoreEntity):
|
|||
state = await self.async_get_last_state()
|
||||
if state is not None:
|
||||
self._state = self.compute_next_state(int(state.state))
|
||||
self._initial = state.attributes.get(ATTR_INITIAL)
|
||||
self._max = state.attributes.get(ATTR_MAXIMUM)
|
||||
self._min = state.attributes.get(ATTR_MINIMUM)
|
||||
self._step = state.attributes.get(ATTR_STEP)
|
||||
|
||||
async def async_decrement(self):
|
||||
"""Decrement the counter."""
|
||||
|
@ -195,6 +202,10 @@ class Counter(RestoreEntity):
|
|||
self._max = kwargs[CONF_MAXIMUM]
|
||||
if CONF_STEP in kwargs:
|
||||
self._step = kwargs[CONF_STEP]
|
||||
if CONF_INITIAL in kwargs:
|
||||
self._initial = kwargs[CONF_INITIAL]
|
||||
if VALUE in kwargs:
|
||||
self._state = kwargs[VALUE]
|
||||
|
||||
self._state = self.compute_next_state(self._state)
|
||||
await self.async_update_ha_state()
|
||||
|
|
|
@ -33,3 +33,9 @@ configure:
|
|||
step:
|
||||
description: New value for step
|
||||
example: 2
|
||||
initial:
|
||||
description: New value for initial
|
||||
example: 6
|
||||
value:
|
||||
description: New state value
|
||||
example: 3
|
||||
|
|
|
@ -174,14 +174,22 @@ def test_initial_state_overrules_restore_state(hass):
|
|||
@asyncio.coroutine
|
||||
def test_restore_state_overrules_initial_state(hass):
|
||||
"""Ensure states are restored on startup."""
|
||||
|
||||
attr = {"initial": 6, "minimum": 1, "maximum": 8, "step": 2}
|
||||
|
||||
mock_restore_cache(
|
||||
hass, (State("counter.test1", "11"), State("counter.test2", "-22"))
|
||||
hass,
|
||||
(
|
||||
State("counter.test1", "11"),
|
||||
State("counter.test2", "-22"),
|
||||
State("counter.test3", "5", attr),
|
||||
),
|
||||
)
|
||||
|
||||
hass.state = CoreState.starting
|
||||
|
||||
yield from async_setup_component(
|
||||
hass, DOMAIN, {DOMAIN: {"test1": {}, "test2": {CONF_INITIAL: 10}}}
|
||||
hass, DOMAIN, {DOMAIN: {"test1": {}, "test2": {CONF_INITIAL: 10}, "test3": {}}}
|
||||
)
|
||||
|
||||
state = hass.states.get("counter.test1")
|
||||
|
@ -192,6 +200,14 @@ def test_restore_state_overrules_initial_state(hass):
|
|||
assert state
|
||||
assert int(state.state) == -22
|
||||
|
||||
state = hass.states.get("counter.test3")
|
||||
assert state
|
||||
assert int(state.state) == 5
|
||||
assert state.attributes.get("initial") == 6
|
||||
assert state.attributes.get("minimum") == 1
|
||||
assert state.attributes.get("maximum") == 8
|
||||
assert state.attributes.get("step") == 2
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_no_initial_state_and_no_restore_state(hass):
|
||||
|
@ -379,11 +395,45 @@ async def test_configure(hass, hass_admin_user):
|
|||
assert state.state == "5"
|
||||
assert 3 == state.attributes.get("step")
|
||||
|
||||
# update value
|
||||
await hass.services.async_call(
|
||||
"counter",
|
||||
"configure",
|
||||
{"entity_id": state.entity_id, "value": 6},
|
||||
True,
|
||||
Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
|
||||
state = hass.states.get("counter.test")
|
||||
assert state is not None
|
||||
assert state.state == "6"
|
||||
|
||||
# update initial
|
||||
await hass.services.async_call(
|
||||
"counter",
|
||||
"configure",
|
||||
{"entity_id": state.entity_id, "initial": 5},
|
||||
True,
|
||||
Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
|
||||
state = hass.states.get("counter.test")
|
||||
assert state is not None
|
||||
assert state.state == "6"
|
||||
assert 5 == state.attributes.get("initial")
|
||||
|
||||
# update all
|
||||
await hass.services.async_call(
|
||||
"counter",
|
||||
"configure",
|
||||
{"entity_id": state.entity_id, "step": 5, "minimum": 0, "maximum": 9},
|
||||
{
|
||||
"entity_id": state.entity_id,
|
||||
"step": 5,
|
||||
"minimum": 0,
|
||||
"maximum": 9,
|
||||
"value": 5,
|
||||
"initial": 6,
|
||||
},
|
||||
True,
|
||||
Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
|
@ -394,3 +444,4 @@ async def test_configure(hass, hass_admin_user):
|
|||
assert 5 == state.attributes.get("step")
|
||||
assert 0 == state.attributes.get("minimum")
|
||||
assert 9 == state.attributes.get("maximum")
|
||||
assert 6 == state.attributes.get("initial")
|
||||
|
|
Loading…
Reference in New Issue