async def test_preset_switch(mock_gateway): """Test a Dynalite preset.""" name = "NAME" preset_name = "PRESET" [device1, device4] = mock_gateway.configure_dyn_dev( { dyn_const.CONF_ACTIVE: False, dyn_const.CONF_AREA: { "1": { dyn_const.CONF_NAME: name, dyn_const.CONF_NO_DEFAULT: True, dyn_const.CONF_PRESET: { "1": { dyn_const.CONF_NAME: preset_name, dyn_const.CONF_FADE: 0.5, }, "4": { dyn_const.CONF_FADE: 0.7 }, }, } }, }, 2, ) assert await mock_gateway.async_setup_dyn_dev() assert device1.category == "switch" assert device4.category == "switch" assert device1.name == f"{name} {preset_name}" assert device4.name == f"{name} Preset 4" assert device1.unique_id == "dynalite_area_1_preset_1" await device1.async_turn_on() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 1, 0.5)) assert device1.is_on assert not device4.is_on await device4.async_turn_on() await asyncio.sleep(0.1) await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 4, 0.7)) assert device4.is_on assert not device1.is_on await device4.async_turn_off() await asyncio.sleep(0.1) await mock_gateway.check_writes([]) assert not device4.is_on assert not device1.is_on await mock_gateway.receive(DynetPacket.select_area_preset_packet( 1, 1, 0.2)) assert not device4.is_on assert device1.is_on await mock_gateway.receive(DynetPacket.report_area_preset_packet(1, 4)) assert device4.is_on assert not device1.is_on
async def test_room_switch(mock_gateway): """Test a room switch with two presets.""" name = "NAME" [on_device, off_device, room_device] = mock_gateway.configure_dyn_dev( { dyn_const.CONF_ACTIVE: False, dyn_const.CONF_AREA: { "1": { dyn_const.CONF_NAME: name, dyn_const.CONF_TEMPLATE: dyn_const.CONF_ROOM, dyn_const.CONF_PRESET: { "1": {}, "4": {} }, } }, }, 3, ) assert await mock_gateway.async_setup_dyn_dev() for device in [on_device, off_device, room_device]: assert device.category == "switch" assert room_device.name == name assert device.unique_id == "dynalite_area_1_room_switch" assert device.available await room_device.async_turn_on() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 1, 0)) assert room_device.is_on assert on_device.is_on assert not off_device.is_on await room_device.async_turn_off() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 4, 0)) assert not room_device.is_on assert not on_device.is_on assert off_device.is_on await on_device.async_turn_on() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 1, 0)) assert room_device.is_on assert on_device.is_on assert not off_device.is_on await off_device.async_turn_on() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 4, 0)) assert not room_device.is_on assert not on_device.is_on assert off_device.is_on
async def test_cover_no_channel(mock_gateway): """Test a cover without an associated channel.""" name = "NAME" [cover_device] = mock_gateway.configure_dyn_dev({ dyn_const.CONF_ACTIVE: False, dyn_const.CONF_POLL_TIMER: 0.05, dyn_const.CONF_AREA: { "1": { dyn_const.CONF_NAME: name, dyn_const.CONF_TEMPLATE: dyn_const.CONF_TIME_COVER, dyn_const.CONF_DURATION: 0.5, dyn_const.CONF_OPEN_PRESET: 1, dyn_const.CONF_CLOSE_PRESET: 2, dyn_const.CONF_STOP_PRESET: 3, } }, }) assert await mock_gateway.async_setup_dyn_dev() assert cover_device.category == "cover" # It is closed. Let's open assert cover_device.is_closed await cover_device.async_open_cover() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 1, 0)) await asyncio.sleep(0.25) assert cover_device.is_opening assert 40 < cover_device.current_cover_position < 60 await asyncio.sleep(0.4) assert (not cover_device.is_closed and not cover_device.is_opening and not cover_device.is_closing) assert cover_device.current_cover_position == 100
async def test_dynalite_devices_default_fade(mock_gateway): """Test that default fade works correctly.""" [channel_device, preset_device] = mock_gateway.configure_dyn_dev( { dyn_const.CONF_ACTIVE: False, dyn_const.CONF_DEFAULT: { dyn_const.CONF_FADE: 0.5 }, dyn_const.CONF_AREA: { "1": { dyn_const.CONF_CHANNEL: { "1": {} } } }, dyn_const.CONF_PRESET: { "1": {} }, }, 2, ) assert await mock_gateway.async_setup_dyn_dev() await mock_gateway.check_writes([]) await channel_device.async_turn_on() await mock_gateway.check_single_write( DynetPacket.set_channel_level_packet(1, 1, 1.0, 0.5)) await preset_device.async_turn_on() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 1, 0.5))
async def test_trigger_switch(mock_gateway): """Test a switch that is a single trigger.""" name = "NAME" [trigger_device] = mock_gateway.configure_dyn_dev( { dyn_const.CONF_ACTIVE: False, dyn_const.CONF_AREA: { "1": { dyn_const.CONF_NAME: name, dyn_const.CONF_TEMPLATE: dyn_const.CONF_TRIGGER, } }, }, 1, ) assert await mock_gateway.async_setup_dyn_dev() await mock_gateway.check_single_update(None) assert trigger_device.category == "switch" assert trigger_device.name == name assert trigger_device.unique_id == "dynalite_area_1_preset_1" assert trigger_device.available await trigger_device.async_turn_on() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 1, 0)) await mock_gateway.check_single_update(trigger_device) await mock_gateway.check_notifications([preset_notification(1, 1)]) assert trigger_device.is_on packet_to_send = DynetPacket.report_area_preset_packet(1, 4) await mock_gateway.receive(packet_to_send) await mock_gateway.check_single_update(trigger_device) await mock_gateway.check_notifications([ packet_notification(packet_to_send.raw_msg), preset_notification(1, 4) ]) assert not trigger_device.is_on
async def test_cover_no_channel(mock_gateway): """Test a cover without an associated channel.""" name = "NAME" [cover_device] = mock_gateway.configure_dyn_dev({ dyn_const.CONF_ACTIVE: False, dyn_const.CONF_POLL_TIMER: 0.05, dyn_const.CONF_AREA: { "1": { dyn_const.CONF_NAME: name, dyn_const.CONF_TEMPLATE: dyn_const.CONF_TIME_COVER, dyn_const.CONF_DURATION: 0.5, dyn_const.CONF_OPEN_PRESET: 1, dyn_const.CONF_CLOSE_PRESET: 2, dyn_const.CONF_STOP_PRESET: 3, } }, }) assert await mock_gateway.async_setup_dyn_dev() await mock_gateway.check_single_update(None) assert cover_device.category == "cover" # Initialize to closed - otherwise it takes the first one as the init status packet_to_send = DynetPacket.report_area_preset_packet(1, 2) await mock_gateway.receive(packet_to_send) await mock_gateway.check_updates([cover_device], True) await mock_gateway.check_notifications([ packet_notification(packet_to_send.raw_msg), preset_notification(1, 2) ]) # It is closed. Let's open assert cover_device.is_closed await cover_device.async_open_cover() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 1, 0)) await mock_gateway.check_updates([cover_device], True) await mock_gateway.check_notifications([preset_notification(1, 1)]) await asyncio.sleep(0.25) await mock_gateway.check_updates([cover_device], True) assert cover_device.is_opening assert 40 < cover_device.current_cover_position < 60 await asyncio.sleep(0.4) await mock_gateway.check_updates([cover_device], True) assert (not cover_device.is_closed and not cover_device.is_opening and not cover_device.is_closing) assert cover_device.current_cover_position == 100
def preset_select_func(area, preset): """Create preset selection packet.""" return DynetPacket.select_area_preset_packet(area, preset, 0)
async def test_cover_with_tilt(mock_gateway): """Test the cover tilt functionality.""" name = "NAME" [cover_device] = mock_gateway.configure_dyn_dev({ dyn_const.CONF_ACTIVE: False, dyn_const.CONF_POLL_TIMER: 0.05, dyn_const.CONF_AREA: { "1": { dyn_const.CONF_NAME: name, dyn_const.CONF_TEMPLATE: dyn_const.CONF_TIME_COVER, dyn_const.CONF_DEVICE_CLASS: "blind", dyn_const.CONF_DURATION: 0.5, dyn_const.CONF_TILT_TIME: 0.25, dyn_const.CONF_OPEN_PRESET: 1, dyn_const.CONF_CLOSE_PRESET: 2, dyn_const.CONF_STOP_PRESET: 3, dyn_const.CONF_CHANNEL_COVER: 4, } }, }) assert await mock_gateway.async_setup_dyn_dev() assert cover_device.category == "cover" assert cover_device.device_class == "blind" assert cover_device.has_tilt # It is closed. Let's open assert cover_device.is_closed assert cover_device.current_cover_tilt_position == 0 asyncio.create_task(cover_device.async_open_cover_tilt()) await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 1, 0)) await asyncio.sleep(0.125) assert cover_device.is_opening assert 30 < cover_device.current_cover_tilt_position < 70 # Stop halfway await cover_device.async_stop_cover_tilt() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 3, 0)) await cover_device.async_open_cover_tilt() assert cover_device.current_cover_tilt_position == 100 assert 30 < cover_device.current_cover_position < 70 assert (not cover_device.is_closed and not cover_device.is_opening and not cover_device.is_closing) await asyncio.sleep(0.1) mock_gateway.reset() asyncio.create_task(cover_device.async_open_cover_tilt()) await mock_gateway.check_writes([]) # It is open. Now let's close mock_gateway.reset() asyncio.create_task(cover_device.async_close_cover_tilt()) await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 2, 0)) await asyncio.sleep(0.125) assert cover_device.is_closing assert 30 < cover_device.current_cover_tilt_position < 70 # Stop halfway await cover_device.async_stop_cover_tilt() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 3, 0)) assert 30 < cover_device.current_cover_tilt_position < 70 assert (not cover_device.is_closed and not cover_device.is_opening and not cover_device.is_closing) # And continue to full close await cover_device.async_close_cover() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 2, 0)) await asyncio.sleep(0.6) assert cover_device.is_closed assert cover_device.current_cover_tilt_position == 0 mock_gateway.reset() asyncio.create_task(cover_device.async_close_cover_tilt()) await mock_gateway.check_writes([]) # Now open it half-way await cover_device.async_set_cover_tilt_position(tilt_position=50) await asyncio.sleep(0.3) assert (not cover_device.is_closed and not cover_device.is_opening and not cover_device.is_closing) assert 30 < cover_device.current_cover_tilt_position < 70
async def test_cover_no_tilt(mock_gateway): """Test basic cover functionality.""" name = "NAME" [ channel_device, open_device, close_device, stop_device, cover_device, ] = mock_gateway.configure_dyn_dev( { dyn_const.CONF_ACTIVE: False, dyn_const.CONF_POLL_TIMER: 0.05, dyn_const.CONF_AREA: { "1": { dyn_const.CONF_NAME: name, dyn_const.CONF_TEMPLATE: dyn_const.CONF_TIME_COVER, dyn_const.CONF_DURATION: 0.5, dyn_const.CONF_OPEN_PRESET: 1, dyn_const.CONF_CLOSE_PRESET: 2, dyn_const.CONF_STOP_PRESET: 3, dyn_const.CONF_CHANNEL_COVER: 4, dyn_const.CONF_CHANNEL: { "4": {} }, dyn_const.CONF_PRESET: { "1": {}, "2": {}, "3": {} }, } }, }, 5, ) assert await mock_gateway.async_setup_dyn_dev() assert channel_device.category == "light" assert open_device.category == "switch" assert close_device.category == "switch" assert stop_device.category == "switch" assert cover_device.category == "cover" assert cover_device.name == name assert cover_device.unique_id == "dynalite_area_1_time_cover" assert cover_device.available assert cover_device.area_name == name assert cover_device.get_master_area == name assert not cover_device.has_tilt assert cover_device.device_class == dyn_const.DEFAULT_COVER_CLASS # It is closed. Let's open assert cover_device.is_closed await cover_device.async_open_cover() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 1, 0)) assert open_device.is_on assert not close_device.is_on assert not stop_device.is_on await asyncio.sleep(0.25) assert cover_device.is_opening assert 40 < cover_device.current_cover_position < 60 await asyncio.sleep(0.4) assert (not cover_device.is_closed and not cover_device.is_opening and not cover_device.is_closing) assert cover_device.current_cover_position == 100 # It is open. Now let's close await cover_device.async_close_cover() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 2, 0)) assert close_device.is_on assert not open_device.is_on assert not stop_device.is_on await asyncio.sleep(0.25) assert cover_device.is_closing assert 40 < cover_device.current_cover_position < 60 # Stop halfway await cover_device.async_stop_cover() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 3, 0)) assert stop_device.is_on assert not open_device.is_on assert not close_device.is_on assert (not cover_device.is_closed and not cover_device.is_opening and not cover_device.is_closing) # And continue to full close await cover_device.async_close_cover() await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 2, 0)) await asyncio.sleep(0.4) assert cover_device.is_closed assert cover_device.current_cover_position == 0 # Now open it half-way await cover_device.async_set_cover_position(position=50) assert (not cover_device.is_closed and not cover_device.is_opening and not cover_device.is_closing) assert 40 < cover_device.current_cover_position < 60 await cover_device.async_set_cover_position(position=25) assert (not cover_device.is_closed and not cover_device.is_opening and not cover_device.is_closing) assert 15 < cover_device.current_cover_position < 35 await cover_device.async_open_cover() await asyncio.sleep(0.01) assert cover_device.is_opening mock_gateway.reset() await cover_device.async_set_cover_position( position=cover_device.current_cover_position) await mock_gateway.check_single_write( DynetPacket.select_area_preset_packet(1, 3, 0)) assert (not cover_device.is_closed and not cover_device.is_opening and not cover_device.is_closing) # Now send commands await mock_gateway.receive(DynetPacket.report_area_preset_packet(1, 1)) assert cover_device.is_opening await mock_gateway.receive(DynetPacket.report_area_preset_packet(1, 2)) assert cover_device.is_closing await mock_gateway.receive(DynetPacket.report_area_preset_packet(1, 3)) assert not cover_device.is_closing and not cover_device.is_opening await mock_gateway.receive(DynetPacket.report_area_preset_packet(1, 1)) await asyncio.sleep(0.3) await mock_gateway.receive( DynetPacket.report_channel_level_packet(1, 4, 0, 1)) assert cover_device.is_closing await asyncio.sleep(0.01) # Open and then stop with channel await mock_gateway.receive( DynetPacket.report_channel_level_packet(1, 4, 1, 0)) assert cover_device.is_opening await mock_gateway.receive(DynetPacket.stop_channel_fade_packet(1, 4)) assert not cover_device.is_opening assert not cover_device.is_closing # Open and then stop with area (channel=0xff) await mock_gateway.receive( DynetPacket.report_channel_level_packet(1, 4, 0, 1)) await mock_gateway.receive( DynetPacket.report_channel_level_packet(1, 4, 1, 0)) assert cover_device.is_opening await mock_gateway.receive(DynetPacket.stop_channel_fade_packet(1, 256)) assert not cover_device.is_opening assert not cover_device.is_closing