<s> """<STR_LIT>""" <EOL> import unittest <EOL> from homeassistant . bootstrap import _setup_component <EOL> from homeassistant . const import STATE_ON , STATE_OFF , ATTR_ASSUMED_STATE <EOL> import homeassistant . components . switch as switch <EOL> from tests . common import ( <EOL> mock_mqtt_component , fire_mqtt_message , get_test_home_assistant ) <EOL> class TestSensorMQTT ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . hass = get_test_home_assistant ( ) <EOL> self . mock_publish = mock_mqtt_component ( self . hass ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . hass . stop ( ) <EOL> def test_controlling_state_via_topic ( self ) : <EOL> """<STR_LIT>""" <EOL> self . hass . config . components = [ '<STR_LIT>' ] <EOL> assert _setup_component ( self . hass , switch . DOMAIN , { <EOL> switch . DOMAIN : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:0> <EOL> } <EOL> } ) <EOL> state = self . hass . states . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( STATE_OFF , state . state ) <EOL> self . assertIsNone ( state . attributes . get ( ATTR_ASSUMED_STATE ) ) <EOL> fire_mqtt_message ( self . hass , '<STR_LIT>' , '<STR_LIT:1>' ) <EOL> self . hass . pool . block_till_done ( ) <EOL> state = self . hass . states . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( STATE_ON , state . state ) <EOL> fire_mqtt_message ( self . hass , '<STR_LIT>' , '<STR_LIT:0>' ) <EOL> self . hass . pool . block_till_done ( ) <EOL> state = self . hass . states . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( STATE_OFF , state . state ) <EOL> def test_sending_mqtt_commands_and_optimistic ( self ) : <EOL> """<STR_LIT>""" <EOL> self . hass . config . components = [ '<STR_LIT>' ] <EOL> assert _setup_component ( self . hass , switch . DOMAIN , { <EOL> switch . DOMAIN : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' <EOL> } <EOL> } ) <EOL> state = self . hass . states . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( STATE_OFF , state . state ) <EOL> self . assertTrue ( state . attributes . get ( ATTR_ASSUMED_STATE ) ) <EOL> switch . turn_on ( self . hass , '<STR_LIT>' ) <EOL> self . hass . pool . block_till_done ( ) <EOL> self . assertEqual ( ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:2> , False ) , <EOL> self . mock_publish . mock_calls [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> state = self . hass . states . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( STATE_ON , state . state ) <EOL> switch . turn_off ( self . hass , '<STR_LIT>' ) <EOL> self . hass . pool . block_till_done ( ) <EOL> self . assertEqual ( ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:2> , False ) , <EOL> self . mock_publish . mock_calls [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> state = self . hass . states . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( STATE_OFF , state . state ) <EOL> def test_controlling_state_via_topic_and_json_message ( self ) : <EOL> """<STR_LIT>""" <EOL> self . hass . config . components = [ '<STR_LIT>' ] <EOL> assert _setup_component ( self . hass , switch . DOMAIN , { <EOL> switch . DOMAIN : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT:test>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> } ) <EOL> state = self . hass . states . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( STATE_OFF , state . state ) <EOL> fire_mqtt_message ( self . hass , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . hass . pool . block_till_done ( ) <EOL> state = self . hass . states . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( STATE_ON , state . state ) <EOL> fire_mqtt_message ( self . hass , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . hass . pool . block_till_done ( ) <EOL> state = self . hass . states . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( STATE_OFF , state . state ) </s>
