Use brightness scaling util in HomeWizard Number entity (#105147)
This commit is contained in:
parent
f48e948871
commit
f8c216a5ea
3 changed files with 8 additions and 5 deletions
|
@ -6,6 +6,7 @@ from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import PERCENTAGE, EntityCategory
|
from homeassistant.const import PERCENTAGE, EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.util.color import brightness_to_value, value_to_brightness
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
from .coordinator import HWEnergyDeviceUpdateCoordinator
|
||||||
|
@ -45,7 +46,9 @@ class HWEnergyNumberEntity(HomeWizardEntity, NumberEntity):
|
||||||
@homewizard_exception_handler
|
@homewizard_exception_handler
|
||||||
async def async_set_native_value(self, value: float) -> None:
|
async def async_set_native_value(self, value: float) -> None:
|
||||||
"""Set a new value."""
|
"""Set a new value."""
|
||||||
await self.coordinator.api.state_set(brightness=int(value * (255 / 100)))
|
await self.coordinator.api.state_set(
|
||||||
|
brightness=value_to_brightness((0, 100), value)
|
||||||
|
)
|
||||||
await self.coordinator.async_refresh()
|
await self.coordinator.async_refresh()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -61,4 +64,4 @@ class HWEnergyNumberEntity(HomeWizardEntity, NumberEntity):
|
||||||
or (brightness := self.coordinator.data.state.brightness) is None
|
or (brightness := self.coordinator.data.state.brightness) is None
|
||||||
):
|
):
|
||||||
return None
|
return None
|
||||||
return round(brightness * (100 / 255))
|
return brightness_to_value((0, 100), brightness)
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
'entity_id': 'number.device_status_light_brightness',
|
'entity_id': 'number.device_status_light_brightness',
|
||||||
'last_changed': <ANY>,
|
'last_changed': <ANY>,
|
||||||
'last_updated': <ANY>,
|
'last_updated': <ANY>,
|
||||||
'state': '100',
|
'state': '100.0',
|
||||||
})
|
})
|
||||||
# ---
|
# ---
|
||||||
# name: test_number_entities[HWE-SKT].1
|
# name: test_number_entities[HWE-SKT].1
|
||||||
|
|
|
@ -41,7 +41,7 @@ async def test_number_entities(
|
||||||
assert snapshot == device_entry
|
assert snapshot == device_entry
|
||||||
|
|
||||||
# Test unknown handling
|
# Test unknown handling
|
||||||
assert state.state == "100"
|
assert state.state == "100.0"
|
||||||
|
|
||||||
mock_homewizardenergy.state.return_value.brightness = None
|
mock_homewizardenergy.state.return_value.brightness = None
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ async def test_number_entities(
|
||||||
)
|
)
|
||||||
|
|
||||||
assert len(mock_homewizardenergy.state_set.mock_calls) == 1
|
assert len(mock_homewizardenergy.state_set.mock_calls) == 1
|
||||||
mock_homewizardenergy.state_set.assert_called_with(brightness=127)
|
mock_homewizardenergy.state_set.assert_called_with(brightness=129)
|
||||||
|
|
||||||
mock_homewizardenergy.state_set.side_effect = RequestError
|
mock_homewizardenergy.state_set.side_effect = RequestError
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
|
|
Loading…
Add table
Reference in a new issue