async def test_init(hass): """Test platform setup.""" await setup.async_setup_component(hass, "persistent_notification", {}) mock_entry = MockConfigEntry(domain=DOMAIN) mock_entry.add_to_hass(hass) mock_light_1 = pyzerproc.Light("AA:BB:CC:DD:EE:FF", "LEDBlue-CCDDEEFF") mock_light_2 = pyzerproc.Light("11:22:33:44:55:66", "LEDBlue-33445566") mock_state_1 = pyzerproc.LightState(False, (0, 0, 0)) mock_state_2 = pyzerproc.LightState(True, (0, 80, 255)) with patch( "homeassistant.components.zerproc.light.pyzerproc.discover", return_value=[mock_light_1, mock_light_2], ), patch.object(mock_light_1, "connect"), patch.object( mock_light_2, "connect" ), patch.object( mock_light_1, "get_state", return_value=mock_state_1 ), patch.object( mock_light_2, "get_state", return_value=mock_state_2 ): await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() state = hass.states.get("light.ledblue_ccddeeff") assert state.state == STATE_OFF assert state.attributes == { ATTR_FRIENDLY_NAME: "LEDBlue-CCDDEEFF", ATTR_SUPPORTED_FEATURES: SUPPORT_BRIGHTNESS | SUPPORT_COLOR, ATTR_ICON: "mdi:string-lights", } state = hass.states.get("light.ledblue_33445566") assert state.state == STATE_ON assert state.attributes == { ATTR_FRIENDLY_NAME: "LEDBlue-33445566", ATTR_SUPPORTED_FEATURES: SUPPORT_BRIGHTNESS | SUPPORT_COLOR, ATTR_ICON: "mdi:string-lights", ATTR_BRIGHTNESS: 255, ATTR_HS_COLOR: (221.176, 100.0), ATTR_RGB_COLOR: (0, 80, 255), ATTR_XY_COLOR: (0.138, 0.08), } with patch.object(hass.loop, "stop"), patch.object( mock_light_1, "disconnect" ) as mock_disconnect_1, patch.object( mock_light_2, "disconnect" ) as mock_disconnect_2: await hass.async_stop() assert mock_disconnect_1.called assert mock_disconnect_2.called
async def run(): light = pyzerproc.Light(address) try: await light.connect() await light.turn_off() finally: await light.disconnect()
async def run(): light = pyzerproc.Light(address) try: await light.connect() state = await light.get_state() click.echo(state.is_on) finally: await light.disconnect()
async def run(): light = pyzerproc.Light(address) try: await light.connect() state = await light.get_state() click.echo(hexlify(bytes(state.color))) finally: await light.disconnect()
def turn_off(address): """Turn off the light with the given MAC address""" light = pyzerproc.Light(address) try: light.connect() light.turn_off() finally: light.disconnect() return 0
async def run(): light = pyzerproc.Light(address) r, g, b = tuple(int(color[i:i + 2], 16) for i in (0, 2, 4)) try: await light.connect() await light.set_color(r, g, b) finally: await light.disconnect()
def get_color(address): """Get the current color of the light""" light = pyzerproc.Light(address) try: light.connect() state = light.get_state() click.echo(hexlify(bytes(state.color))) finally: light.disconnect() return 0
def is_on(address): """Get the current on/off status of the light""" light = pyzerproc.Light(address) try: light.connect() state = light.get_state() click.echo(state.is_on) finally: light.disconnect() return 0
def set_color(address, color): """Set the light with the given MAC address to an RRGGBB hex color""" light = pyzerproc.Light(address) r, g, b = tuple(int(color[i:i + 2], 16) for i in (0, 2, 4)) try: light.connect() light.set_color(r, g, b) finally: light.disconnect() return 0
async def test_connect_exception(hass): """Test platform setup.""" await setup.async_setup_component(hass, "persistent_notification", {}) mock_entry = MockConfigEntry(domain=DOMAIN) mock_entry.add_to_hass(hass) mock_light = pyzerproc.Light("AA:BB:CC:DD:EE:FF", "LEDBlue-CCDDEEFF") with patch( "homeassistant.components.zerproc.light.pyzerproc.discover", return_value=[mock_light], ), patch.object( mock_light, "connect", side_effect=pyzerproc.ZerprocException("TEST") ): await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() # The exception should be captured and no entities should be added assert len(hass.data[DOMAIN]["addresses"]) == 0
async def mock_light(hass): """Create a mock light entity.""" await setup.async_setup_component(hass, "persistent_notification", {}) mock_entry = MockConfigEntry(domain=DOMAIN) mock_entry.add_to_hass(hass) light = pyzerproc.Light("AA:BB:CC:DD:EE:FF", "LEDBlue-CCDDEEFF") mock_state = pyzerproc.LightState(False, (0, 0, 0)) with patch( "homeassistant.components.zerproc.light.pyzerproc.discover", return_value=[light], ), patch.object(light, "connect"), patch.object( light, "get_state", return_value=mock_state ): await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() return light