async def help_test_entity_id_update_discovery_update(hass, mqtt_mock, domain, config): """Test MQTT discovery update after entity_id is updated.""" entity_reg = await hass.helpers.entity_registry.async_get_registry() config = copy.deepcopy(config) data = json.dumps(config) topic = get_topic_tele_will(config) async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data) await hass.async_block_till_done() async_fire_mqtt_message(hass, topic, config_get_state_online(config)) state = hass.states.get(f"{domain}.test") assert state.state != STATE_UNAVAILABLE async_fire_mqtt_message(hass, topic, config_get_state_offline(config)) state = hass.states.get(f"{domain}.test") assert state.state == STATE_UNAVAILABLE entity_reg.async_update_entity(f"{domain}.test", new_entity_id=f"{domain}.milk") await hass.async_block_till_done() assert hass.states.get(f"{domain}.milk") assert config[CONF_PREFIX][PREFIX_TELE] != "tele2" config[CONF_PREFIX][PREFIX_TELE] = "tele2" data = json.dumps(config) async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data) await hass.async_block_till_done() assert len(hass.states.async_entity_ids(domain)) == 1 topic = get_topic_tele_will(config) async_fire_mqtt_message(hass, topic, config_get_state_online(config)) state = hass.states.get(f"{domain}.milk") assert state.state != STATE_UNAVAILABLE
async def help_test_availability_poll_state( hass, mqtt_client_mock, mqtt_mock, domain, config, poll_topic, poll_payload ): """Test polling of state when device is available. This is a test helper for the TasmotaAvailability mixin. """ async_fire_mqtt_message( hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", json.dumps(config), ) await hass.async_block_till_done() mqtt_mock.async_publish.reset_mock() # Device online, verify poll for state async_fire_mqtt_message( hass, get_topic_tele_will(config), config_get_state_online(config), ) await hass.async_block_till_done() await hass.async_block_till_done() await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with(poll_topic, poll_payload, 0, False) mqtt_mock.async_publish.reset_mock() # Disconnected from MQTT server mqtt_mock.connected = False await hass.async_add_executor_job(mqtt_client_mock.on_disconnect, None, None, 0) await hass.async_block_till_done() await hass.async_block_till_done() await hass.async_block_till_done() assert not mqtt_mock.async_publish.called # Reconnected to MQTT server mqtt_mock.connected = True await hass.async_add_executor_job(mqtt_client_mock.on_connect, None, None, None, 0) await hass.async_block_till_done() await hass.async_block_till_done() await hass.async_block_till_done() assert not mqtt_mock.async_publish.called # Device online, verify poll for state async_fire_mqtt_message( hass, get_topic_tele_will(config), config_get_state_online(config), ) await hass.async_block_till_done() await hass.async_block_till_done() await hass.async_block_till_done() mqtt_mock.async_publish.assert_called_once_with(poll_topic, poll_payload, 0, False)
async def help_test_entity_id_update_discovery_update(opp, mqtt_mock, domain, config, sensor_config=None, entity_id="test"): """Test MQTT discovery update after entity_id is updated.""" entity_reg = er.async_get(opp) config = copy.deepcopy(config) data = json.dumps(config) topic = get_topic_tele_will(config) async_fire_mqtt_message(opp, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data) await opp.async_block_till_done() if sensor_config: async_fire_mqtt_message( opp, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/sensors", json.dumps(sensor_config), ) await opp.async_block_till_done() async_fire_mqtt_message(opp, topic, config_get_state_online(config)) state = opp.states.get(f"{domain}.{entity_id}") assert state.state != STATE_UNAVAILABLE async_fire_mqtt_message(opp, topic, config_get_state_offline(config)) state = opp.states.get(f"{domain}.{entity_id}") assert state.state == STATE_UNAVAILABLE entity_reg.async_update_entity(f"{domain}.{entity_id}", new_entity_id=f"{domain}.milk") await opp.async_block_till_done() assert opp.states.get(f"{domain}.milk") assert config[CONF_PREFIX][PREFIX_TELE] != "tele2" config[CONF_PREFIX][PREFIX_TELE] = "tele2" data = json.dumps(config) async_fire_mqtt_message(opp, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data) await opp.async_block_till_done() assert len(opp.states.async_entity_ids(domain)) == 1 topic = get_topic_tele_will(config) async_fire_mqtt_message(opp, topic, config_get_state_online(config)) state = opp.states.get(f"{domain}.milk") assert state.state != STATE_UNAVAILABLE
async def help_test_availability_when_connection_lost( hass, mqtt_client_mock, mqtt_mock, domain, config ): """Test availability after MQTT disconnection. This is a test helper for the TasmotaAvailability mixin. """ async_fire_mqtt_message( hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", json.dumps(config), ) await hass.async_block_till_done() # Device online async_fire_mqtt_message( hass, get_topic_tele_will(config), config_get_state_online(config), ) state = hass.states.get(f"{domain}.test") assert state.state != STATE_UNAVAILABLE # Disconnected from MQTT server -> state changed to unavailable mqtt_mock.connected = False await hass.async_add_executor_job(mqtt_client_mock.on_disconnect, None, None, 0) await hass.async_block_till_done() await hass.async_block_till_done() await hass.async_block_till_done() state = hass.states.get(f"{domain}.test") assert state.state == STATE_UNAVAILABLE # Reconnected to MQTT server -> state still unavailable mqtt_mock.connected = True await hass.async_add_executor_job(mqtt_client_mock.on_connect, None, None, None, 0) await hass.async_block_till_done() await hass.async_block_till_done() await hass.async_block_till_done() state = hass.states.get(f"{domain}.test") assert state.state == STATE_UNAVAILABLE # Receive LWT again async_fire_mqtt_message( hass, get_topic_tele_will(config), config_get_state_online(config), ) state = hass.states.get(f"{domain}.test") assert state.state != STATE_UNAVAILABLE
async def help_test_entity_id_update_subscriptions( hass, mqtt_mock, domain, config, topics=None ): """Test MQTT subscriptions are managed when entity_id is updated.""" entity_reg = await hass.helpers.entity_registry.async_get_registry() config = copy.deepcopy(config) data = json.dumps(config) mqtt_mock.async_subscribe.reset_mock() async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", data) await hass.async_block_till_done() if not topics: topics = [get_topic_tele_state(config), get_topic_tele_will(config)] assert len(topics) > 0 state = hass.states.get(f"{domain}.test") assert state is not None assert mqtt_mock.async_subscribe.call_count == len(topics) for topic in topics: mqtt_mock.async_subscribe.assert_any_call(topic, ANY, ANY, ANY) mqtt_mock.async_subscribe.reset_mock() entity_reg.async_update_entity(f"{domain}.test", new_entity_id=f"{domain}.milk") await hass.async_block_till_done() state = hass.states.get(f"{domain}.test") assert state is None state = hass.states.get(f"{domain}.milk") assert state is not None for topic in topics: mqtt_mock.async_subscribe.assert_any_call(topic, ANY, ANY, ANY)
async def help_test_availability( hass, mqtt_mock, domain, config, sensor_config=None, entity_id="test", ): """Test availability. This is a test helper for the TasmotaAvailability mixin. """ async_fire_mqtt_message( hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", json.dumps(config), ) await hass.async_block_till_done() if sensor_config: async_fire_mqtt_message( hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/sensors", json.dumps(sensor_config), ) await hass.async_block_till_done() state = hass.states.get(f"{domain}.{entity_id}") assert state.state == STATE_UNAVAILABLE async_fire_mqtt_message( hass, get_topic_tele_will(config), config_get_state_online(config), ) state = hass.states.get(f"{domain}.{entity_id}") assert state.state != STATE_UNAVAILABLE async_fire_mqtt_message( hass, get_topic_tele_will(config), config_get_state_offline(config), ) state = hass.states.get(f"{domain}.{entity_id}") assert state.state == STATE_UNAVAILABLE
async def test_entity_id_update_subscriptions(hass, mqtt_mock, setup_tasmota): """Test MQTT subscriptions are managed when entity_id is updated.""" config = copy.deepcopy(DEFAULT_CONFIG) config["rl"][0] = 1 topics = [ get_topic_stat_result(config), get_topic_tele_state(config), get_topic_tele_will(config), ] await help_test_entity_id_update_subscriptions( hass, mqtt_mock, switch.DOMAIN, config, topics )
async def test_entity_id_update_subscriptions(hass, mqtt_mock, setup_tasmota): """Test MQTT subscriptions are managed when entity_id is updated.""" config = copy.deepcopy(DEFAULT_CONFIG) config["swc"][0] = 1 config["swn"][0] = "Test" topics = [ get_topic_stat_result(config), get_topic_tele_sensor(config), get_topic_stat_status(config, 10), get_topic_tele_will(config), ] await help_test_entity_id_update_subscriptions(hass, mqtt_mock, binary_sensor.DOMAIN, config, topics)
async def test_entity_id_update_subscriptions(opp, mqtt_mock, setup_tasmota): """Test MQTT subscriptions are managed when entity_id is updated.""" config = copy.deepcopy(DEFAULT_CONFIG) config["dn"] = "Test" config["rl"][0] = 3 config["rl"][1] = 3 topics = [ get_topic_stat_result(config), get_topic_tele_sensor(config), get_topic_stat_status(config, 10), get_topic_tele_will(config), ] await help_test_entity_id_update_subscriptions( opp, mqtt_mock, cover.DOMAIN, config, topics, entity_id="test_cover_1" )
async def test_entity_id_update_subscriptions(hass, mqtt_mock, setup_tasmota): """Test MQTT subscriptions are managed when entity_id is updated.""" config = copy.deepcopy(DEFAULT_CONFIG) sensor_config = copy.deepcopy(DEFAULT_SENSOR_CONFIG) topics = [ get_topic_tele_sensor(config), get_topic_stat_status(config, 10), get_topic_tele_will(config), ] await help_test_entity_id_update_subscriptions( hass, mqtt_mock, Platform.SENSOR, config, topics, sensor_config, "tasmota_dht11_temperature", )
async def test_availability_poll_state_once( opp, mqtt_client_mock, mqtt_mock, setup_tasmota ): """Test several entities send a single message to update state.""" config = copy.deepcopy(DEFAULT_CONFIG) config["rl"][0] = 1 config["rl"][1] = 1 config["swc"][0] = 1 config["swc"][1] = 1 poll_payload_relay = "" poll_payload_switch = "10" poll_topic_relay = "tasmota_49A3BC/cmnd/STATE" poll_topic_switch = "tasmota_49A3BC/cmnd/STATUS" async_fire_mqtt_message( opp, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config", json.dumps(config), ) await opp.async_block_till_done() mqtt_mock.async_publish.reset_mock() # Device online, verify poll for state async_fire_mqtt_message( opp, get_topic_tele_will(config), config_get_state_online(config), ) await opp.async_block_till_done() await opp.async_block_till_done() await opp.async_block_till_done() mqtt_mock.async_publish.assert_has_calls( [ call(poll_topic_relay, poll_payload_relay, 0, False), call(poll_topic_switch, poll_payload_switch, 0, False), ], any_order=True, )
async def help_test_availability_discovery_update( hass, mqtt_mock, domain, config, sensor_config=None, entity_id="test", ): """Test update of discovered TasmotaAvailability. This is a test helper for the TasmotaAvailability mixin. """ # customize availability topic config1 = copy.deepcopy(config) config1[CONF_PREFIX][PREFIX_TELE] = "tele1" config1[CONF_OFFLINE] = "offline1" config1[CONF_ONLINE] = "online1" config2 = copy.deepcopy(config) config2[CONF_PREFIX][PREFIX_TELE] = "tele2" config2[CONF_OFFLINE] = "offline2" config2[CONF_ONLINE] = "online2" data1 = json.dumps(config1) data2 = json.dumps(config2) availability_topic1 = get_topic_tele_will(config1) availability_topic2 = get_topic_tele_will(config2) assert availability_topic1 != availability_topic2 offline1 = config_get_state_offline(config1) offline2 = config_get_state_offline(config2) assert offline1 != offline2 online1 = config_get_state_online(config1) online2 = config_get_state_online(config2) assert online1 != online2 async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config1[CONF_MAC]}/config", data1) await hass.async_block_till_done() if sensor_config: async_fire_mqtt_message( hass, f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/sensors", json.dumps(sensor_config), ) await hass.async_block_till_done() state = hass.states.get(f"{domain}.{entity_id}") assert state.state == STATE_UNAVAILABLE async_fire_mqtt_message(hass, availability_topic1, online1) state = hass.states.get(f"{domain}.{entity_id}") assert state.state != STATE_UNAVAILABLE async_fire_mqtt_message(hass, availability_topic1, offline1) state = hass.states.get(f"{domain}.{entity_id}") assert state.state == STATE_UNAVAILABLE # Change availability settings async_fire_mqtt_message(hass, f"{DEFAULT_PREFIX}/{config2[CONF_MAC]}/config", data2) await hass.async_block_till_done() # Verify we are no longer subscribing to the old topic or payload async_fire_mqtt_message(hass, availability_topic1, online1) async_fire_mqtt_message(hass, availability_topic1, online2) async_fire_mqtt_message(hass, availability_topic2, online1) state = hass.states.get(f"{domain}.{entity_id}") assert state.state == STATE_UNAVAILABLE # Verify we are subscribing to the new topic async_fire_mqtt_message(hass, availability_topic2, online2) state = hass.states.get(f"{domain}.{entity_id}") assert state.state != STATE_UNAVAILABLE