コード例 #1
0
 def test_name_not_set(self, mock_update):
     """Return the superclass name property if not set in configuration."""
     config = copy(TEST_CONFIG['sensor'])
     del config[tcp.CONF_NAME]
     entity = Entity()
     sensor = tcp.TcpSensor(self.hass, config)
     assert sensor.name == entity.name
コード例 #2
0
 def test_update_sends_payload(self, mock_select, mock_socket):
     """Send the configured payload as bytes."""
     tcp.TcpSensor(self.hass, TEST_CONFIG['sensor'])
     mock_socket = mock_socket().__enter__()
     mock_socket.send.assert_called_with(
         TEST_CONFIG['sensor'][tcp.CONF_PAYLOAD].encode()
     )
コード例 #3
0
 def test_update_connects_to_host_and_port(self, mock_select, mock_socket):
     """Connect to the configured host and port."""
     tcp.TcpSensor(self.hass, TEST_CONFIG['sensor'])
     mock_socket = mock_socket().__enter__()
     assert mock_socket.connect.mock_calls[0][1] == ((
         TEST_CONFIG['sensor'][tcp.CONF_HOST],
         TEST_CONFIG['sensor'][tcp.CONF_PORT]),)
コード例 #4
0
    def test_config_valid_keys(self, *args):
        """Store valid keys in _config."""
        sensor = tcp.TcpSensor(self.hass, TEST_CONFIG['sensor'])
        del TEST_CONFIG['sensor']['platform']

        for key in TEST_CONFIG['sensor']:
            assert key in sensor._config
コード例 #5
0
 def test_config_invalid_keys(self, mock_update):
     """Shouldn't store invalid keys in _config."""
     config = copy(TEST_CONFIG['sensor'])
     config.update({'a': 'test_a', 'b': 'test_b', 'c': 'test_c'})
     sensor = tcp.TcpSensor(self.hass, config)
     for invalid_key in 'abc':
         assert invalid_key not in sensor._config
コード例 #6
0
 def test_update_renders_value_in_template(self, mock_select, mock_socket):
     """Render the value in the provided template."""
     test_value = 'test_value'
     mock_socket = mock_socket().__enter__()
     mock_socket.recv.return_value = test_value.encode()
     config = copy(TEST_CONFIG['sensor'])
     config[tcp.CONF_VALUE_TEMPLATE] = Template('{{ value }} {{ 1+1 }}')
     sensor = tcp.TcpSensor(self.hass, config)
     assert sensor._state == '%s 2' % test_value
コード例 #7
0
 def test_update_returns_if_template_render_fails(
         self, mock_select, mock_socket):
     """Return None if rendering the template fails."""
     test_value = 'test_value'
     mock_socket = mock_socket().__enter__()
     mock_socket.recv.return_value = test_value.encode()
     config = copy(TEST_CONFIG['sensor'])
     config[tcp.CONF_VALUE_TEMPLATE] = Template("{{ this won't work")
     sensor = tcp.TcpSensor(self.hass, config)
     assert sensor.update() is None
コード例 #8
0
 def test_update_receives_packet_and_sets_as_state(
         self, mock_select, mock_socket):
     """Test the response from the socket and set it as the state."""
     test_value = 'test_value'
     mock_socket = mock_socket().__enter__()
     mock_socket.recv.return_value = test_value.encode()
     config = copy(TEST_CONFIG['sensor'])
     del config[tcp.CONF_VALUE_TEMPLATE]
     sensor = tcp.TcpSensor(self.hass, config)
     assert sensor._state == test_value
コード例 #9
0
    def test_config_uses_defaults(self, mock_update):
        """Check if defaults were set."""
        config = copy(TEST_CONFIG['sensor'])

        for key in KEYS_AND_DEFAULTS:
            del config[key]

        with assert_setup_component(1) as result_config:
            assert setup_component(self.hass, 'sensor', {
                'sensor': config,
            })

        sensor = tcp.TcpSensor(self.hass, result_config['sensor'][0])

        for key, default in KEYS_AND_DEFAULTS.items():
            assert sensor._config[key] == default
コード例 #10
0
 def test_unit_of_measurement(self, mock_update):
     """Return the configured unit of measurement."""
     sensor = tcp.TcpSensor(self.hass, TEST_CONFIG['sensor'])
     assert sensor.unit_of_measurement == \
         TEST_CONFIG['sensor'][tcp.CONF_UNIT_OF_MEASUREMENT]
コード例 #11
0
 def test_state(self, mock_update):
     """Return the contents of _state."""
     sensor = tcp.TcpSensor(self.hass, TEST_CONFIG['sensor'])
     uuid = str(uuid4())
     sensor._state = uuid
     assert sensor.state == uuid
コード例 #12
0
 def test_name(self, mock_update):
     """Return the name if set in the configuration."""
     sensor = tcp.TcpSensor(self.hass, TEST_CONFIG['sensor'])
     assert sensor.name == TEST_CONFIG['sensor'][tcp.CONF_NAME]
コード例 #13
0
 def test_update_calls_select_with_timeout(self, mock_select, mock_socket):
     """Provide the timeout argument to select."""
     tcp.TcpSensor(self.hass, TEST_CONFIG['sensor'])
     mock_socket = mock_socket().__enter__()
     mock_select.assert_called_with(
         [mock_socket], [], [], TEST_CONFIG['sensor'][tcp.CONF_TIMEOUT])
コード例 #14
0
 def test_update_returns_if_select_fails(self, *args):
     """Return if select fails to return a socket."""
     with patch('homeassistant.components.sensor.tcp.TcpSensor.update'):
         sensor = tcp.TcpSensor(self.hass, TEST_CONFIG['sensor'])
     assert sensor.update() is None
コード例 #15
0
 def test_update_returns_if_connecting_fails(self, *args):
     """Return if connecting to host fails."""
     with patch('homeassistant.components.sensor.tcp.TcpSensor.update'):
         sensor = tcp.TcpSensor(self.hass, TEST_CONFIG['sensor'])
     assert sensor.update() is None
コード例 #16
0
 def test_init_calls_update(self, mock_update):
     """Call update() method during __init__()."""
     tcp.TcpSensor(self.hass, TEST_CONFIG)
     assert mock_update.called