mirror of
https://github.com/elisspace/core.git
synced 2026-09-13 14:37:05 +00:00
Add binary sensor platform to LIFX integration (#77535)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
committed by
Paulus Schoutsen
parent
9387449abf
commit
ea0b406692
@@ -22,10 +22,13 @@ DEFAULT_ENTRY_TITLE = LABEL
|
||||
class MockMessage:
|
||||
"""Mock a lifx message."""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, **kwargs):
|
||||
"""Init message."""
|
||||
self.target_addr = SERIAL
|
||||
self.count = 9
|
||||
for k, v in kwargs.items():
|
||||
if k != "callb":
|
||||
setattr(self, k, v)
|
||||
|
||||
|
||||
class MockFailingLifxCommand:
|
||||
@@ -50,15 +53,20 @@ class MockFailingLifxCommand:
|
||||
class MockLifxCommand:
|
||||
"""Mock a lifx command."""
|
||||
|
||||
def __name__(self):
|
||||
"""Return name."""
|
||||
return "mock_lifx_command"
|
||||
|
||||
def __init__(self, bulb, **kwargs):
|
||||
"""Init command."""
|
||||
self.bulb = bulb
|
||||
self.calls = []
|
||||
self.msg_kwargs = kwargs
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
"""Call command."""
|
||||
if callb := kwargs.get("callb"):
|
||||
callb(self.bulb, MockMessage())
|
||||
callb(self.bulb, MockMessage(**self.msg_kwargs))
|
||||
self.calls.append([args, kwargs])
|
||||
|
||||
def reset_mock(self):
|
||||
@@ -108,6 +116,20 @@ def _mocked_brightness_bulb() -> Light:
|
||||
return bulb
|
||||
|
||||
|
||||
def _mocked_clean_bulb() -> Light:
|
||||
bulb = _mocked_bulb()
|
||||
bulb.get_hev_cycle = MockLifxCommand(
|
||||
bulb, duration=7200, remaining=0, last_power=False
|
||||
)
|
||||
bulb.hev_cycle = {
|
||||
"duration": 7200,
|
||||
"remaining": 30,
|
||||
"last_power": False,
|
||||
}
|
||||
bulb.product = 90
|
||||
return bulb
|
||||
|
||||
|
||||
def _mocked_light_strip() -> Light:
|
||||
bulb = _mocked_bulb()
|
||||
bulb.product = 31 # LIFX Z
|
||||
|
||||
74
tests/components/lifx/test_binary_sensor.py
Normal file
74
tests/components/lifx/test_binary_sensor.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""Test the lifx binary sensor platwform."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from homeassistant.components import lifx
|
||||
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
CONF_HOST,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
STATE_UNKNOWN,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.entity import EntityCategory
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import (
|
||||
DEFAULT_ENTRY_TITLE,
|
||||
IP_ADDRESS,
|
||||
MAC_ADDRESS,
|
||||
SERIAL,
|
||||
_mocked_clean_bulb,
|
||||
_patch_config_flow_try_connect,
|
||||
_patch_device,
|
||||
_patch_discovery,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
|
||||
async def test_hev_cycle_state(hass: HomeAssistant) -> None:
|
||||
"""Test HEV cycle state binary sensor."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=lifx.DOMAIN,
|
||||
title=DEFAULT_ENTRY_TITLE,
|
||||
data={CONF_HOST: IP_ADDRESS},
|
||||
unique_id=MAC_ADDRESS,
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
bulb = _mocked_clean_bulb()
|
||||
with _patch_discovery(device=bulb), _patch_config_flow_try_connect(
|
||||
device=bulb
|
||||
), _patch_device(device=bulb):
|
||||
await async_setup_component(hass, lifx.DOMAIN, {lifx.DOMAIN: {}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_id = "binary_sensor.my_bulb_clean_cycle"
|
||||
entity_registry = er.async_get(hass)
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state
|
||||
assert state.state == STATE_ON
|
||||
assert state.attributes.get(ATTR_DEVICE_CLASS) == BinarySensorDeviceClass.RUNNING
|
||||
|
||||
entry = entity_registry.async_get(entity_id)
|
||||
assert state
|
||||
assert entry.unique_id == f"{SERIAL}_hev_cycle_state"
|
||||
assert entry.entity_category == EntityCategory.DIAGNOSTIC
|
||||
|
||||
bulb.hev_cycle = {"duration": 7200, "remaining": 0, "last_power": False}
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=30))
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(entity_id).state == STATE_OFF
|
||||
|
||||
bulb.hev_cycle = None
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=30))
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(entity_id).state == STATE_UNKNOWN
|
||||
Reference in New Issue
Block a user