Add number entity value property (#43902)

pull/43914/head
Martin Hjelmare 2020-12-03 18:35:17 +01:00 committed by GitHub
parent 54cb2d42af
commit 8d33c2092f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 7 deletions

View File

@ -98,7 +98,7 @@ class DemoNumber(NumberEntity):
return self._assumed
@property
def state(self):
def value(self):
"""Return the current value."""
return self._state

View File

@ -1,4 +1,5 @@
"""Component to allow numeric input for platforms."""
from abc import abstractmethod
from datetime import timedelta
import logging
from typing import Any, Dict
@ -93,6 +94,16 @@ class NumberEntity(Entity):
step /= 10.0
return step
@property
def state(self) -> float:
"""Return the entity state."""
return self.value
@property
@abstractmethod
def value(self) -> float:
"""Return the entity value to represent the entity state."""
def set_value(self, value: float) -> None:
"""Set new value."""
raise NotImplementedError()

View File

@ -1,8 +1,17 @@
"""The tests for the Number component."""
from unittest.mock import MagicMock
from homeassistant.components.number import NumberEntity
from tests.async_mock import MagicMock
class MockDefaultNumberEntity(NumberEntity):
"""Mock NumberEntity device to use in tests."""
@property
def value(self):
"""Return the current value."""
return 0.5
class MockNumberEntity(NumberEntity):
"""Mock NumberEntity device to use in tests."""
@ -13,14 +22,14 @@ class MockNumberEntity(NumberEntity):
return 1.0
@property
def state(self):
def value(self):
"""Return the current value."""
return "0.5"
return 0.5
async def test_step(hass):
"""Test the step calculation."""
number = NumberEntity()
number = MockDefaultNumberEntity()
assert number.step == 1.0
number_2 = MockNumberEntity()
@ -29,7 +38,7 @@ async def test_step(hass):
async def test_sync_set_value(hass):
"""Test if async set_value calls sync set_value."""
number = NumberEntity()
number = MockDefaultNumberEntity()
number.hass = hass
number.set_value = MagicMock()