Add light platform to Linear garage door (#111426)

* Add light platform

* Fix light test

* Suggestions by CFenner

* Fix tests

* More fixes

* Revert test changes

* Undo base entity

* Rebase

* Fix to use base entity

* Fix name

* More fixes

* Fix tests

* Add translation key

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
IceBotYT 2024-05-15 15:43:31 -04:00 committed by GitHub
parent 38c2688ec2
commit ebb02a7081
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 441 additions and 5 deletions

View file

@ -9,7 +9,7 @@ from homeassistant.core import HomeAssistant
from .const import DOMAIN from .const import DOMAIN
from .coordinator import LinearUpdateCoordinator from .coordinator import LinearUpdateCoordinator
PLATFORMS: list[Platform] = [Platform.COVER] PLATFORMS: list[Platform] = [Platform.COVER, Platform.LIGHT]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

View file

@ -0,0 +1,80 @@
"""Linear garage door light."""
from typing import Any
from linear_garage_door import Linear
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .coordinator import LinearUpdateCoordinator
from .entity import LinearEntity
SUPPORTED_SUBDEVICES = ["Light"]
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Linear Garage Door cover."""
coordinator: LinearUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
data = coordinator.data
async_add_entities(
LinearLightEntity(
device_id=device_id,
device_name=data[device_id].name,
sub_device_id=subdev,
coordinator=coordinator,
)
for device_id in data
for subdev in data[device_id].subdevices
if subdev in SUPPORTED_SUBDEVICES
)
class LinearLightEntity(LinearEntity, LightEntity):
"""Light for Linear devices."""
_attr_color_mode = ColorMode.BRIGHTNESS
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
_attr_translation_key = "light"
@property
def is_on(self) -> bool:
"""Return if the light is on or not."""
return bool(self.sub_device["On_B"] == "true")
@property
def brightness(self) -> int | None:
"""Return the brightness of the light."""
return round(int(self.sub_device["On_P"]) / 100 * 255)
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light."""
async def _turn_on(linear: Linear) -> None:
"""Turn on the light."""
if not kwargs:
await linear.operate_device(self._device_id, self._sub_device_id, "On")
elif ATTR_BRIGHTNESS in kwargs:
brightness = round((kwargs[ATTR_BRIGHTNESS] / 255) * 100)
await linear.operate_device(
self._device_id, self._sub_device_id, f"DimPercent:{brightness}"
)
await self.coordinator.execute(_turn_on)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the light."""
await self.coordinator.execute(
lambda linear: linear.operate_device(
self._device_id, self._sub_device_id, "Off"
)
)

View file

@ -16,5 +16,12 @@
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
} }
},
"entity": {
"light": {
"light": {
"name": "[%key:component::light::title%]"
}
}
} }
} }

View file

@ -5,8 +5,8 @@
"Opening_P": "100" "Opening_P": "100"
}, },
"Light": { "Light": {
"On_B": "true", "On_B": "false",
"On_P": "100" "On_P": "0"
} }
}, },
"test2": { "test2": {
@ -15,8 +15,8 @@
"Opening_P": "0" "Opening_P": "0"
}, },
"Light": { "Light": {
"On_B": "false", "On_B": "true",
"On_P": "0" "On_P": "100"
} }
}, },
"test3": { "test3": {

View file

@ -0,0 +1,225 @@
# serializer version: 1
# name: test_data[light.test_garage_1_light-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.test_garage_1_light',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Light',
'platform': 'linear_garage_door',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'light',
'unique_id': 'test1-Light',
'unit_of_measurement': None,
})
# ---
# name: test_data[light.test_garage_1_light-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'brightness': 255,
'color_mode': <ColorMode.BRIGHTNESS: 'brightness'>,
'friendly_name': 'Test Garage 1 Light',
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
'supported_features': <LightEntityFeature: 0>,
}),
'context': <ANY>,
'entity_id': 'light.test_garage_1_light',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_data[light.test_garage_2_light-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.test_garage_2_light',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Light',
'platform': 'linear_garage_door',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'light',
'unique_id': 'test2-Light',
'unit_of_measurement': None,
})
# ---
# name: test_data[light.test_garage_2_light-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'brightness': None,
'color_mode': None,
'friendly_name': 'Test Garage 2 Light',
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
'supported_features': <LightEntityFeature: 0>,
}),
'context': <ANY>,
'entity_id': 'light.test_garage_2_light',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_data[light.test_garage_3_light-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.test_garage_3_light',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Light',
'platform': 'linear_garage_door',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'light',
'unique_id': 'test3-Light',
'unit_of_measurement': None,
})
# ---
# name: test_data[light.test_garage_3_light-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'brightness': None,
'color_mode': None,
'friendly_name': 'Test Garage 3 Light',
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
'supported_features': <LightEntityFeature: 0>,
}),
'context': <ANY>,
'entity_id': 'light.test_garage_3_light',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_data[light.test_garage_4_light-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.test_garage_4_light',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Light',
'platform': 'linear_garage_door',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'light',
'unique_id': 'test4-Light',
'unit_of_measurement': None,
})
# ---
# name: test_data[light.test_garage_4_light-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'brightness': 255,
'color_mode': <ColorMode.BRIGHTNESS: 'brightness'>,
'friendly_name': 'Test Garage 4 Light',
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
'supported_features': <LightEntityFeature: 0>,
}),
'context': <ANY>,
'entity_id': 'light.test_garage_4_light',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---

View file

@ -0,0 +1,124 @@
"""Test Linear Garage Door light."""
from datetime import timedelta
from unittest.mock import AsyncMock
from freezegun.api import FrozenDateTimeFactory
from syrupy import SnapshotAssertion
from homeassistant.components.light import (
DOMAIN as LIGHT_DOMAIN,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.components.linear_garage_door import DOMAIN
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_BRIGHTNESS,
STATE_OFF,
STATE_ON,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import setup_integration
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_json_object_fixture,
snapshot_platform,
)
async def test_data(
hass: HomeAssistant,
mock_linear: AsyncMock,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test that data gets parsed and returned appropriately."""
await setup_integration(hass, mock_config_entry, [Platform.LIGHT])
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_turn_on(
hass: HomeAssistant, mock_linear: AsyncMock, mock_config_entry: MockConfigEntry
) -> None:
"""Test that turning on the light works as intended."""
await setup_integration(hass, mock_config_entry, [Platform.LIGHT])
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.test_garage_2_light"},
blocking=True,
)
assert mock_linear.operate_device.call_count == 1
async def test_turn_on_with_brightness(
hass: HomeAssistant, mock_linear: AsyncMock, mock_config_entry: MockConfigEntry
) -> None:
"""Test that turning on the light works as intended."""
await setup_integration(hass, mock_config_entry, [Platform.LIGHT])
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.test_garage_2_light", CONF_BRIGHTNESS: 50},
blocking=True,
)
mock_linear.operate_device.assert_called_once_with(
"test2", "Light", "DimPercent:20"
)
async def test_turn_off(
hass: HomeAssistant, mock_linear: AsyncMock, mock_config_entry: MockConfigEntry
) -> None:
"""Test that turning off the light works as intended."""
await setup_integration(hass, mock_config_entry, [Platform.LIGHT])
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.test_garage_1_light"},
blocking=True,
)
assert mock_linear.operate_device.call_count == 1
async def test_update_light_state(
hass: HomeAssistant,
mock_linear: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test that turning off the light works as intended."""
await setup_integration(hass, mock_config_entry, [Platform.LIGHT])
assert hass.states.get("light.test_garage_1_light").state == STATE_ON
assert hass.states.get("light.test_garage_2_light").state == STATE_OFF
device_states = load_json_object_fixture("get_device_state_1.json", DOMAIN)
mock_linear.get_device_state.side_effect = lambda device_id: device_states[
device_id
]
freezer.tick(timedelta(seconds=60))
async_fire_time_changed(hass)
assert hass.states.get("light.test_garage_1_light").state == STATE_OFF
assert hass.states.get("light.test_garage_2_light").state == STATE_ON