Use _attr_is_on in fibaro light (#131211)

pull/127580/merge
rappenze 2024-11-22 17:10:38 +01:00 committed by GitHub
parent 0460046d32
commit 1dbb92e7f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 97 additions and 24 deletions

View File

@ -132,32 +132,25 @@ class FibaroLight(FibaroEntity, LightEntity):
"""Turn the light 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:
"""Update the state."""
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
if brightness_supported(self.supported_color_modes):
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:
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]
else:
self._attr_rgbw_color = rgbw

View File

@ -106,6 +106,29 @@ def mock_cover() -> Mock:
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
def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
"""Return the default mocked config entry."""

View 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"