mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Use _attr_is_on in fibaro light (#131211)
This commit is contained in:
parent
0460046d32
commit
1dbb92e7f3
@ -132,32 +132,25 @@ class FibaroLight(FibaroEntity, LightEntity):
|
|||||||
"""Turn the light off."""
|
"""Turn the light off."""
|
||||||
self.call_turn_off()
|
self.call_turn_off()
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self) -> bool | None:
|
|
||||||
"""Return true if device is on.
|
|
||||||
|
|
||||||
Dimmable and RGB lights can be on based on different
|
|
||||||
properties, so we need to check here several values.
|
|
||||||
|
|
||||||
JSON for HC2 uses always string, HC3 uses int for integers.
|
|
||||||
"""
|
|
||||||
if self.current_binary_state:
|
|
||||||
return True
|
|
||||||
with suppress(TypeError):
|
|
||||||
if self.fibaro_device.brightness != 0:
|
|
||||||
return True
|
|
||||||
with suppress(TypeError):
|
|
||||||
if self.fibaro_device.current_program != 0:
|
|
||||||
return True
|
|
||||||
with suppress(TypeError):
|
|
||||||
if self.fibaro_device.current_program_id != 0:
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Update the state."""
|
"""Update the state."""
|
||||||
super().update()
|
super().update()
|
||||||
|
|
||||||
|
# Dimmable and RGB lights can be on based on different
|
||||||
|
# properties, so we need to check here several values
|
||||||
|
# to see if the light is on.
|
||||||
|
light_is_on = self.current_binary_state
|
||||||
|
with suppress(TypeError):
|
||||||
|
if self.fibaro_device.brightness != 0:
|
||||||
|
light_is_on = True
|
||||||
|
with suppress(TypeError):
|
||||||
|
if self.fibaro_device.current_program != 0:
|
||||||
|
light_is_on = True
|
||||||
|
with suppress(TypeError):
|
||||||
|
if self.fibaro_device.current_program_id != 0:
|
||||||
|
light_is_on = True
|
||||||
|
self._attr_is_on = light_is_on
|
||||||
|
|
||||||
# Brightness handling
|
# Brightness handling
|
||||||
if brightness_supported(self.supported_color_modes):
|
if brightness_supported(self.supported_color_modes):
|
||||||
self._attr_brightness = scaleto255(self.fibaro_device.value.int_value())
|
self._attr_brightness = scaleto255(self.fibaro_device.value.int_value())
|
||||||
@ -172,7 +165,7 @@ class FibaroLight(FibaroEntity, LightEntity):
|
|||||||
if rgbw == (0, 0, 0, 0) and self.fibaro_device.last_color_set.has_color:
|
if rgbw == (0, 0, 0, 0) and self.fibaro_device.last_color_set.has_color:
|
||||||
rgbw = self.fibaro_device.last_color_set.rgbw_color
|
rgbw = self.fibaro_device.last_color_set.rgbw_color
|
||||||
|
|
||||||
if self._attr_color_mode == ColorMode.RGB:
|
if self.color_mode == ColorMode.RGB:
|
||||||
self._attr_rgb_color = rgbw[:3]
|
self._attr_rgb_color = rgbw[:3]
|
||||||
else:
|
else:
|
||||||
self._attr_rgbw_color = rgbw
|
self._attr_rgbw_color = rgbw
|
||||||
|
@ -106,6 +106,29 @@ def mock_cover() -> Mock:
|
|||||||
return cover
|
return cover
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_light() -> Mock:
|
||||||
|
"""Fixture for a dimmmable light."""
|
||||||
|
light = Mock()
|
||||||
|
light.fibaro_id = 3
|
||||||
|
light.parent_fibaro_id = 0
|
||||||
|
light.name = "Test light"
|
||||||
|
light.room_id = 1
|
||||||
|
light.dead = False
|
||||||
|
light.visible = True
|
||||||
|
light.enabled = True
|
||||||
|
light.type = "com.fibaro.FGD212"
|
||||||
|
light.base_type = "com.fibaro.device"
|
||||||
|
light.properties = {"manufacturer": ""}
|
||||||
|
light.actions = {"setValue": 1, "on": 0, "off": 0}
|
||||||
|
light.supported_features = {}
|
||||||
|
value_mock = Mock()
|
||||||
|
value_mock.has_value = True
|
||||||
|
value_mock.int_value.return_value = 20
|
||||||
|
light.value = value_mock
|
||||||
|
return light
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
|
def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
|
||||||
"""Return the default mocked config entry."""
|
"""Return the default mocked config entry."""
|
||||||
|
57
tests/components/fibaro/test_light.py
Normal file
57
tests/components/fibaro/test_light.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
"""Test the Fibaro light platform."""
|
||||||
|
|
||||||
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
|
from homeassistant.const import Platform
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
|
from .conftest import init_integration
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
|
async def test_light_setup(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
mock_fibaro_client: Mock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
mock_light: Mock,
|
||||||
|
mock_room: Mock,
|
||||||
|
) -> None:
|
||||||
|
"""Test that the light creates an entity."""
|
||||||
|
|
||||||
|
# Arrange
|
||||||
|
mock_fibaro_client.read_rooms.return_value = [mock_room]
|
||||||
|
mock_fibaro_client.read_devices.return_value = [mock_light]
|
||||||
|
|
||||||
|
with patch("homeassistant.components.fibaro.PLATFORMS", [Platform.LIGHT]):
|
||||||
|
# Act
|
||||||
|
await init_integration(hass, mock_config_entry)
|
||||||
|
# Assert
|
||||||
|
entry = entity_registry.async_get("light.room_1_test_light_3")
|
||||||
|
assert entry
|
||||||
|
assert entry.unique_id == "hc2_111111.3"
|
||||||
|
assert entry.original_name == "Room 1 Test light"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_light_brightness(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_fibaro_client: Mock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
mock_light: Mock,
|
||||||
|
mock_room: Mock,
|
||||||
|
) -> None:
|
||||||
|
"""Test that the light brightness value is translated."""
|
||||||
|
|
||||||
|
# Arrange
|
||||||
|
mock_fibaro_client.read_rooms.return_value = [mock_room]
|
||||||
|
mock_fibaro_client.read_devices.return_value = [mock_light]
|
||||||
|
|
||||||
|
with patch("homeassistant.components.fibaro.PLATFORMS", [Platform.LIGHT]):
|
||||||
|
# Act
|
||||||
|
await init_integration(hass, mock_config_entry)
|
||||||
|
# Assert
|
||||||
|
state = hass.states.get("light.room_1_test_light_3")
|
||||||
|
assert state.attributes["brightness"] == 51
|
||||||
|
assert state.state == "on"
|
Loading…
x
Reference in New Issue
Block a user