<s> """<STR_LIT>""" <EOL> import socket <EOL> from copy import copy <EOL> from uuid import uuid4 <EOL> from unittest . mock import patch , Mock <EOL> from homeassistant . components . sensor import tcp <EOL> from homeassistant . helpers . entity import Entity <EOL> from tests . common import get_test_home_assistant <EOL> TEST_CONFIG = { <EOL> tcp . CONF_NAME : "<STR_LIT>" , <EOL> tcp . CONF_HOST : "<STR_LIT>" , <EOL> tcp . CONF_PORT : <NUM_LIT> , <EOL> tcp . CONF_TIMEOUT : tcp . DEFAULT_TIMEOUT + <NUM_LIT:1> , <EOL> tcp . CONF_PAYLOAD : "<STR_LIT>" , <EOL> tcp . CONF_UNIT : "<STR_LIT>" , <EOL> tcp . CONF_VALUE_TEMPLATE : "<STR_LIT>" , <EOL> tcp . CONF_VALUE_ON : "<STR_LIT>" , <EOL> tcp . CONF_BUFFER_SIZE : tcp . DEFAULT_BUFFER_SIZE + <NUM_LIT:1> <EOL> } <EOL> KEYS_AND_DEFAULTS = { <EOL> tcp . CONF_NAME : None , <EOL> tcp . CONF_TIMEOUT : tcp . DEFAULT_TIMEOUT , <EOL> tcp . CONF_UNIT : None , <EOL> tcp . CONF_VALUE_TEMPLATE : None , <EOL> tcp . CONF_VALUE_ON : None , <EOL> tcp . CONF_BUFFER_SIZE : tcp . DEFAULT_BUFFER_SIZE <EOL> } <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_setup_platform_valid_config ( mock_update ) : <EOL> """<STR_LIT>""" <EOL> add_entities = Mock ( ) <EOL> ret = tcp . setup_platform ( None , TEST_CONFIG , add_entities ) <EOL> assert ret is None , "<STR_LIT>" <EOL> assert add_entities . called <EOL> assert isinstance ( add_entities . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , tcp . Sensor ) <EOL> def test_setup_platform_invalid_config ( ) : <EOL> """<STR_LIT>""" <EOL> config = copy ( TEST_CONFIG ) <EOL> del config [ tcp . CONF_HOST ] <EOL> assert tcp . setup_platform ( None , config , None ) is False <EOL> class TestTCPSensor ( ) : <EOL> """<STR_LIT>""" <EOL> def setup_class ( cls ) : <EOL> """<STR_LIT>""" <EOL> cls . hass = get_test_home_assistant ( ) <EOL> def teardown_class ( cls ) : <EOL> """<STR_LIT>""" <EOL> cls . hass . stop ( ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_name ( self , mock_update ) : <EOL> """<STR_LIT>""" <EOL> sensor = tcp . Sensor ( self . hass , TEST_CONFIG ) <EOL> assert sensor . name == TEST_CONFIG [ tcp . CONF_NAME ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_name_not_set ( self , mock_update ) : <EOL> """<STR_LIT>""" <EOL> config = copy ( TEST_CONFIG ) <EOL> del config [ tcp . CONF_NAME ] <EOL> entity = Entity ( ) <EOL> sensor = tcp . Sensor ( self . hass , config ) <EOL> assert sensor . name == entity . name <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_state ( self , mock_update ) : <EOL> """<STR_LIT>""" <EOL> sensor = tcp . Sensor ( self . hass , TEST_CONFIG ) <EOL> uuid = str ( uuid4 ( ) ) <EOL> sensor . _state = uuid <EOL> assert sensor . state == uuid <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_unit_of_measurement ( self , mock_update ) : <EOL> """<STR_LIT>""" <EOL> sensor = tcp . Sensor ( self . hass , TEST_CONFIG ) <EOL> assert sensor . unit_of_measurement == TEST_CONFIG [ tcp . CONF_UNIT ] <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_config_valid_keys ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> sensor = tcp . Sensor ( self . hass , TEST_CONFIG ) <EOL> for key in TEST_CONFIG : <EOL> assert key in sensor . _config <EOL> def test_validate_config_valid_keys ( self ) : <EOL> """<STR_LIT>""" <EOL> assert tcp . Sensor . validate_config ( TEST_CONFIG ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_config_invalid_keys ( self , mock_update ) : <EOL> """<STR_LIT>""" <EOL> config = copy ( TEST_CONFIG ) <EOL> config . update ( { <EOL> "<STR_LIT:a>" : "<STR_LIT>" , <EOL> "<STR_LIT:b>" : "<STR_LIT>" , <EOL> "<STR_LIT:c>" : "<STR_LIT>" <EOL> } ) <EOL> sensor = tcp . Sensor ( self . hass , config ) <EOL> for invalid_key in "<STR_LIT:abc>" : <EOL> assert invalid_key not in sensor . _config <EOL> def test_validate_config_invalid_keys ( self ) : <EOL> """<STR_LIT>""" <EOL> config = copy ( TEST_CONFIG ) <EOL> config . update ( { <EOL> "<STR_LIT:a>" : "<STR_LIT>" , <EOL> "<STR_LIT:b>" : "<STR_LIT>" , <EOL> "<STR_LIT:c>" : "<STR_LIT>" <EOL> } ) <EOL> assert tcp . Sensor . validate_config ( config ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_config_uses_defaults ( self , mock_update ) : <EOL> """<STR_LIT>""" <EOL> config = copy ( TEST_CONFIG ) <EOL> for key in KEYS_AND_DEFAULTS . keys ( ) : <EOL> del config [ key ] <EOL> sensor = tcp . Sensor ( self . hass , config ) <EOL> for key , default in KEYS_AND_DEFAULTS . items ( ) : <EOL> assert sensor . _config [ key ] == default <EOL> def test_validate_config_missing_defaults ( self ) : <EOL> """<STR_LIT>""" <EOL> config = copy ( TEST_CONFIG ) <EOL> for key in KEYS_AND_DEFAULTS . keys ( ) : <EOL> del config [ key ] <EOL> assert tcp . Sensor . validate_config ( config ) <EOL> def test_validate_config_missing_required ( self ) : <EOL> """<STR_LIT>""" <EOL> for key in TEST_CONFIG : <EOL> if key in KEYS_AND_DEFAULTS : <EOL> continue <EOL> config = copy ( TEST_CONFIG ) <EOL> del config [ key ] <EOL> assert not tcp . Sensor . validate_config ( config ) , ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % key ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_init_calls_update ( self , mock_update ) : <EOL> """<STR_LIT>""" <EOL> tcp . Sensor ( self . hass , TEST_CONFIG ) <EOL> assert mock_update . called <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" , return_value = ( True , False , False ) ) <EOL> def test_update_connects_to_host_and_port ( self , mock_select , mock_socket ) : <EOL> """<STR_LIT>""" <EOL> tcp . Sensor ( self . hass , TEST_CONFIG ) <EOL> mock_socket = mock_socket ( ) . __enter__ ( ) <EOL> assert mock_socket . connect . mock_calls [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] == ( ( <EOL> TEST_CONFIG [ tcp . CONF_HOST ] , <EOL> TEST_CONFIG [ tcp . CONF_PORT ] ) , ) <EOL> @ patch ( "<STR_LIT>" , side_effect = socket . error ( ) ) <EOL> def test_update_returns_if_connecting_fails ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> with patch ( "<STR_LIT>" ) : <EOL> sensor = tcp . Sensor ( self . hass , TEST_CONFIG ) <EOL> assert sensor . update ( ) is None <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" , side_effect = socket . error ( ) ) <EOL> def test_update_returns_if_sending_fails ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> with patch ( "<STR_LIT>" ) : <EOL> sensor = tcp . Sensor ( self . hass , TEST_CONFIG ) <EOL> assert sensor . update ( ) is None <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" , return_value = ( False , False , False ) ) <EOL> def test_update_returns_if_select_fails ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> with patch ( "<STR_LIT>" ) : <EOL> sensor = tcp . Sensor ( self . hass , TEST_CONFIG ) <EOL> assert sensor . update ( ) is None <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" , return_value = ( True , False , False ) ) <EOL> def test_update_sends_payload ( self , mock_select , mock_socket ) : <EOL> """<STR_LIT>""" <EOL> tcp . Sensor ( self . hass , TEST_CONFIG ) <EOL> mock_socket = mock_socket ( ) . __enter__ ( ) <EOL> mock_socket . send . assert_called_with ( <EOL> TEST_CONFIG [ tcp . CONF_PAYLOAD ] . encode ( ) <EOL> ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" , return_value = ( True , False , False ) ) <EOL> def test_update_calls_select_with_timeout ( self , mock_select , mock_socket ) : <EOL> """<STR_LIT>""" <EOL> tcp . Sensor ( self . hass , TEST_CONFIG ) <EOL> mock_socket = mock_socket ( ) . __enter__ ( ) <EOL> mock_select . assert_called_with ( <EOL> [ mock_socket ] , [ ] , [ ] , TEST_CONFIG [ tcp . CONF_TIMEOUT ] ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" , return_value = ( True , False , False ) ) <EOL> def test_update_receives_packet_and_sets_as_state ( <EOL> self , mock_select , mock_socket ) : <EOL> """<STR_LIT>""" <EOL> test_value = "<STR_LIT>" <EOL> mock_socket = mock_socket ( ) . __enter__ ( ) <EOL> mock_socket . recv . return_value = test_value . encode ( ) <EOL> config = copy ( TEST_CONFIG ) <EOL> del config [ tcp . CONF_VALUE_TEMPLATE ] <EOL> sensor = tcp . Sensor ( self . hass , config ) <EOL> assert sensor . _state == test_value <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" , return_value = ( True , False , False ) ) <EOL> def test_update_renders_value_in_template ( self , mock_select , mock_socket ) : <EOL> """<STR_LIT>""" <EOL> test_value = "<STR_LIT>" <EOL> mock_socket = mock_socket ( ) . __enter__ ( ) <EOL> mock_socket . recv . return_value = test_value . encode ( ) <EOL> config = copy ( TEST_CONFIG ) <EOL> config [ tcp . CONF_VALUE_TEMPLATE ] = "<STR_LIT>" <EOL> sensor = tcp . Sensor ( self . hass , config ) <EOL> assert sensor . _state == "<STR_LIT>" % test_value <EOL> @ patch ( "<STR_LIT>" ) <EOL> @ patch ( "<STR_LIT>" , return_value = ( True , False , False ) ) <EOL> def test_update_returns_if_template_render_fails ( <EOL> self , mock_select , mock_socket ) : <EOL> """<STR_LIT>""" <EOL> test_value = "<STR_LIT>" <EOL> mock_socket = mock_socket ( ) . __enter__ ( ) <EOL> mock_socket . recv . return_value = test_value . encode ( ) <EOL> config = copy ( TEST_CONFIG ) <EOL> config [ tcp . CONF_VALUE_TEMPLATE ] = "<STR_LIT>" <EOL> sensor = tcp . Sensor ( self . hass , config ) <EOL> assert sensor . update ( ) is None </s>
